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
Bitwise Operator
What is a bit? A bit is a representation of 1 or 0. Basically OFF(0) and ON(1)
What is a byte? A byte is made up of 8 bits and the highest value of a byte is 255, which would mean every bit is set. We will look at why a byte's maximum value is 255.
This representation of 1 Byte
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255 (1 Byte)
A few examples for better understanding
The "AND" operator:
&
This would output the number 8. Why? Well let's see using our table example.
So you can see from the table the only bit they share together is the 8 bit.
Second example
The two shared bits are 32 and 4, which when added together return 36.
The "Or" operator:
|
This would output the number 11. Why?
You will notice that we have 3 bits set, in the 8, 2, and 1 columns. Add those up: 8+2+1=11.
QUESTION:
What does
=>
mean?ANSWER:
=>
Is the symbol we humans decided to use to separate"Key" => "Value"
pairs in Associative Arrays.ELABORATING:
To understand this, we have to know what Associative Arrays are. The first thing that comes up when a conventional programmer thinks of an array (in PHP) would be something similar to:
Where as, if we wanted to call the array in some later part of the code, we could do:
So far so good. However, as humans, we might find it hard to remember that index
[0]
of the array is the value of the year 2016, index[1]
of the array is a greetings, and index[2]
of the array is a simple integer value. The alternative we would then have is to use what is called an Associative Array. An Associative array has a few differences from a Sequential Array (which is what the previous cases were since they increment the index used in a predetermined sequence, by incrementing by 1 for each following value).Differences (between a sequential and associative array):
Durring the declaration of an Associative Array, you don't only include the
value
of what you want to put in the array, but you also put the index value (called thekey
) which you want to use when calling the array in later parts of the code. The following syntax is used during it's declaration:"key" => "value"
.When using the Associative Array, the
key
value would then be placed inside the index of the array to retrieve the desiredvalue
.For instance:
And now, to receive the same output as before, the
key
value would be used in the arrays index:FINAL POINT:
So from the above example, it is pretty easy to see that the
=>
symbol is used to express the relationship of an Associative Array between each of thekey
andvalue
pairs in an array DURING the initiation of the values within the array.Three DOTS as Splat Operator (...) (since PHP 5.6)
PHP has an operator "..." (Three dots) which is referred as Splat Operator. It is used to pass arbitrary number of parameters in a function and this type of function is called Variadic Functions. Let’s take examples to use of "..." (Three dots).
Each arguments of calculateNumbers() function pass through $params as an array when use "… ".
There are many different ways to use "… " operator. Below some examples:
_
Alias for gettext()The underscore character '_' as in
_()
is an alias to thegettext()
function.Spaceship Operator
<=>
(Added in PHP 7)Examples for
<=>
Spaceship operator (PHP 7, Source: PHP Manual):Integers, Floats, Strings, Arrays & objects for Three-way comparison of variables.