What is this?
This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
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, 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 to the help others provided.
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
??
Null Coalesce Operator (since PHP 7)
?string
?int
?array
?bool
?float
Nullable return type declaration (since PHP 7.1)
:
Alternative syntax for control structures, Ternary Operator
- 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 and how do you say it when reading code out loud?
- 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 <>
- PHP's <> operator
- 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
...
Argument unpacking (since PHP 5.6)
**
Exponentiation (since PHP 5.6)
#
One-line shell-style comment
{}
Curly bracesAnd some words about last post
Question:
What does "&" mean here in PHP?
PHP "&" operator
Makes life more easier once we get used to it..(check example below carefully)
& usually checks bits that are set in both $a and $b are set.
So behind all above is game of bitwise operator and bits.
One usefull case of these is easy configurations like give below, so a single integer field can store thousands of combos for you.
Most people have already read the docs but didn't reliase the real world use case of these bitwise operators.
Example That you 'll love
<=>
Spaceship OperatorAdded in PHP 7
The spaceship operator
<=>
is the latest comparison operator added in PHP 7. It is a non-associative binary operator with the same precedence as equality operators (==
,!=
,===
,!==
). This operator allows for simpler three-way comparison between left-hand and right-hand operands.The operator results in an integer expression of:
0
when both operands are equal0
when the left-hand operand is less than the right-hand operand0
when the left-hand operand is greater than the right-hand operande.g.
A good practical application of using this operator would be in comparison type callbacks that are expected to return a zero, negative, or positive integer based on a three-way comparison between two values. The comparison function passed to
usort
is one such example.Before PHP 7 you would write...
Since PHP 7 you can write...
Null Coalesce operator "??" (Added in PHP 7)
Not the catchiest name for an operator, but PHP 7 brings in the rather handy null coalesce so I thought I'd share an example.
In PHP 5, we already have a ternary operator, which tests a value, and then returns the second element if that returns true and the third if it doesn't:
There is also a shorthand for that which allows you to skip the second element if it's the same as the first one: echo $count ?: 10; // also outputs 10
In PHP 7 we additionally get the ?? operator which rather than indicating extreme confusion which is how I would usually use two question marks together instead allows us to chain together a string of values. Reading from left to right, the first value which exists and is not null is the value that will be returned.
This construct is useful for giving priority to one or more values coming perhaps from user input or existing configuration, and safely falling back on a given default if that configuration is missing. It's kind of a small feature but it's one that I know I'll be using as soon as my applications upgrade to PHP 7.
Nullable return type declaration
PHP 7 adds support for return type declarations. Similarly to argument type declarations, return type declarations specify the type of value that will be returned from a function. The same types are available for return type declarations as are available for argument type declarations.
Strict typing also has an effect on return type declarations. In the default weak mode, returned values will be coerced to the correct type if they are not already of that type. In strong mode, the returned value must be of the correct type, otherwise, a TypeError will be thrown.
As of PHP 7.1.0, return values can be marked as nullable by prefixing the type name with a question mark (?). This signifies that the function returns either the specified type or NULL.
Source
An overview of operators in PHP:
Logical Operators:
Comparison operators:
Arithmetic Operators:
Incrementing/Decrementing Operators:
Bitwise Operators:
String Operators:
Array Operators:
Assignment Operators:
Note
and
operator andor
operator have lower precedence than assignment operator=
.This means that
$a = true and false;
is equivalent to($a = true) and false
.In most cases you will probably want to use
&&
and||
, which behave in a way known from languages like C, Java or JavaScript.