What is this?
This is a collection of questions that come up now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
This question shouldn't be used as a dupe target for closing questions. Use actual questions linked here as dupe targets instead.
Why is this?
It used to be hard to find questions about operators and other syntax tokens.¹
The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, and not to copy over content from the PHP Manual.
Note: Since January 2013, Stack Overflow does support special characters. Just surround the search terms by quotes, e.g. [php] "==" vs "==="
What should I do here?
If you have been pointed here by someone because you have asked such a question, please find the particular syntax below. The linked pages to the PHP manual along with the linked questions will likely answer your question then. If so, you are encouraged to upvote the answer. This list is not meant as a substitute for the help others provide.
The List
If your particular token is not listed below, you might find it in the List of Parser Tokens.
&
Bitwise Operators or References
- What does it mean to start a PHP function with an ampersand?
- Understanding PHP & (ampersand, bitwise and) operator
- PHP "&" operator
- Difference between & and && in PHP
- What does "&" mean here in PHP?
- What does "&" mean in this case?
- What does the "&" sign mean in PHP?
- What does this signature mean (&) in PHP?
- How does the "&" operator work in a PHP function?
- What does & in &2 mean in PHP?
- When should I use a bitwise operator?
- Is there ever a need to use ampersand in front of an object? (&$)
=&
References
- Reference assignment operator in PHP, =&
- What do the "=&" and "&=" operators in PHP mean?
- What do the '&=' and '=&' operators do?
- What does =& mean in PHP?
- 'AND' vs '&&' as operator
- Difference between & and && in PHP
- Is there any difference between "and" and "&&" operators in PHP?
- PHP - and / or keywords
- What does the percent sign mean in PHP?
- What is the PHP operator % and how do I use it in real-world examples?
- What is the use of the @ symbol in PHP?
- 'At' symbol before variable name in PHP: @$_POST
- PHP functions and @functions
- Should I use @ in my PHP code?
- What does @ mean in PHP?
- What are the PHP operators "?" and ":" called and what do they do?
- ?: operator (the 'Elvis operator') in PHP
- Where can I read about conditionals done with "?" and ":" (colon)?
- Using PHP 5.3 ?: operator
Double question mark
??
Null Coalesce Operator (since PHP 7)
A question mark followed by a type declaration
?string
?int
?array
?bool
?float
Nullable type declaration (since PHP 7.1)
?->
question mark followed by object operator is a NullSafe Operator (since PHP 8.0)
:
Alternative syntax for control structures, Ternary Operator, Return Type Declaration
- What do two colons mean in PHP?
- What's the meaning of the PHP token name T_PAAMAYIM_NEKUDOTAYIM?
- What's the difference between :: (double colon) and -> (arrow) in PHP?
- What exactly are late static bindings in PHP?
- static::staticFunctionName()
- Unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_NS_Separator
- What is the "->" PHP operator called?
- Where do we use the object operator "->" in PHP?
- What's the difference between :: (double colon) and -> (arrow) in PHP?
- What does the PHP syntax $var1->$var2 mean?
- What does "->" mean/refer to in PHP?
=>
Arrays
- What does <<<END mean in PHP?
- PHP expression <<<EOB
- In PHP, what does "<<<" represent?
- Using <<<CON in PHP
- What's this kind of syntax in PHP?
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- PHP != and == operators
- The 3 different equals
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
- What does "===" mean?
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- The 3 different equals
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
- PHP != and == operators
- Is there a difference between !== and != in PHP?
- comparing, !== versus !=
- What is the difference between <> and !=
- PHP operator <>
- https://stackoverflow.com/questions/589391
- What is the difference between <> and !=
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
<=>
Comparison Operators (since PHP 7.0)
- What is the difference between the | and || operators?
- What Does Using A Single Pipe '|' In A Function Argument Do?
- What is the difference between the | and || operators?
- PHP - and / or keywords
- What exactly does || mean?
- The behaviour of the or operator in PHP
+
Arithmetic Operators, Array Operators
+=
and -=
Assignment Operators
++
and --
Incrementing/Decrementing Operators
- Difference between period and comma when concatenating with echo versus return?
- What does a . (dot) do in PHP?
- What does $$ (dollar dollar or double dollar) mean in PHP?
- what is "$$" in PHP
- $function() and $$variable
<?=
Short Open Tags
[]
Arrays (short syntax since PHP 5.4)
- PHP arrays... What is/are the meaning(s) of an empty bracket?
- What is the meaning of []
- Php array_push() vs myArray[]
- What does [] mean when reading from a PHP array?
- Shorthand for arrays: literal
$var = []
empty array
..
Double-dot character range
...
Argument unpacking (since PHP 5.6)
**
Exponentiation (since PHP 5.6)
#
One-line shell-style comment
#[]
Attributes (since PHP 8)
ベストアンサー1
Incrementing / Decrementing Operators
++
increment operator
--
decrement operator
Example Name Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.
These can go before or after the variable.
If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.
For example:
$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}
In the case above ++$i
is used, since it is faster. $i++
would have the same results.
Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.
However, you must use $apples--
, since first, you want to display the current number of apples, and then you want to subtract one from it.
You can also increment letters in PHP:
$i = "a";
while ($i < "c") {
echo $i++;
}
Once z
is reached aa
is next, and so on.
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
Stack Overflow Posts: