operators - Reference — What does this symbol mean in PHP? -


what this?

this collection of questions come every , syntax in php. community wiki, invited participate in maintaining list.

why this?

it used hard find questions operators , other syntax tokens.¹
main idea have links existing questions on stack overflow, it's easier reference them, not copy on content php manual.

¹ note: since january 2013, stack overflow does support special characters. surround search terms quotes, e.g. [php] "==" vs "==="

what should here?

if have been pointed here because have asked such question, please find particular syntax below. linked pages php manual along linked questions answer question then. if so, encouraged upvote answer. list not meant substitute others provided.

the list

if particular token not listed below, might find in list of parser tokens.


& bitwise operators or references


=& references


&= bitwise operators


&& logical operators


% arithmetic operators


!! logical operators


@ error control operators


?: ternary operator


?? null coalesce operator (since php 7)


: alternative syntax control structures, ternary operator


:: scope resolution operator


\ namespaces


-> classes , objects


=> arrays


^ bitwise operators


>> bitwise operators


<< bitwise operators


<<< heredoc or nowdoc


= assignment operators


== comparison operators


=== comparison operators


!== comparison operators


!= comparison operators


<> comparison operators


<=> comparison operators (since php 7.0)


| bitwise operators


|| logical operators


~ bitwise operators


+ arithmetic operators, array operators


+= , -= assignment operators


++ , -- incrementing/decrementing operators


.= assignment operators


. string operators


, function arguments

, variable declarations


$$ variable variables


` execution operator


<?= short open tags


[] arrays (since php 5.4)


<? opening , closing tags


... argument unpacking (since php 5.6)


** exponentiation (since php 5.6)


# one-line shell-style comment


incrementing / decrementing operators

++ increment operator

-- decrement operator

example    name              effect --------------------------------------------------------------------- ++$a       pre-increment     increments $a one, returns $a. $a++       post-increment    returns $a, increments $a one. --$a       pre-decrement     decrements $a one, returns $a. $a--       post-decrement    returns $a, decrements $a one. 

these can go before or after variable.

if put before variable, increment/decrement operation done variable first result returned. if put after variable, variable first returned, increment/decrement operation done.

for example:

$apples = 10; ($i = 0; $i < 10; ++$i) {     echo 'i have ' . $apples-- . " apples. ate one.\n"; } 

live example

in case above ++$i used, since faster. $i++ have same results.

pre-increment little bit faster, because increments variable , after 'returns' result. post-increment creates special variable, copies there value of first variable , after first variable used, replaces value second's.

however, must use $apples--, since first want display current number of apples, , then want subtract 1 it.

you can increment letters in php:

$i = "a"; while ($i < "c") {     echo $i++; } 

once z reached aa next, , on.

note character variables can incremented not decremented , plain ascii characters (a-z , a-z) supported.


stack overflow posts:


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -