Reference — What does this symbol mean in PHP?
Solution 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:
Solution 2
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.
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
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: &
$a = 9;
$b = 10;
echo $a & $b;
This would output the number 8. Why? Well let's see using our table example.
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
| $a | 0| 0| 0| 0| 1| 0| 0| 1|
-------------------------------------------
| $b | 0| 0| 0| 0| 1| 0| 1| 0|
-------------------------------------------
| & | 0| 0| 0| 0| 1| 0| 0| 0|
-------------------------------------------
So you can see from the table the only bit they share together is the 8 bit.
Second example
$a = 36;
$b = 103;
echo $a & $b; // This would output the number 36.
$a = 00100100
$b = 01100111
The two shared bits are 32 and 4, which when added together return 36.
The "Or" operator: |
$a = 9;
$b = 10;
echo $a | $b;
This would output the number 11. Why?
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
| $a | 0| 0| 0| 0| 1| 0| 0| 1|
-------------------------------------------
| $b | 0| 0| 0| 0| 1| 0| 1| 0|
-------------------------------------------
| | | 0| 0| 0| 0| 1| 0| 1| 1|
-------------------------------------------
You will notice that we have 3 bits set, in the 8, 2, and 1 columns. Add those up: 8+2+1=11.
Solution 3
<=>
Spaceship Operator
Added 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 equal - Less than
0
when the left-hand operand is less than the right-hand operand - Greater than
0
when the left-hand operand is greater than the right-hand operand
e.g.
1 <=> 1; // 0
1 <=> 2; // -1
2 <=> 1; // 1
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...
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
if ($a < $b) {
return -1;
} elseif ($a > $b) {
return 1;
} else {
return 0;
}
});
Since PHP 7 you can write...
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
return $a <=> $b;
// return $b <=> $a; // for reversing order
});
Solution 4
The underscore character '_' as in _()
is an alias to the gettext()
function.
Solution 5
Syntax | Name | Description |
---|---|---|
x == y |
Equality |
true if x and y have the same key/value pairs |
x != y |
Inequality |
true if x is not equal to y |
x === y |
Identity |
true if x and y have the same key/value pairsin the same order and of the same types |
x !== y |
Non-identity |
true if x is not identical to y |
x <=> y |
Spaceship | 0 if x is equal to y, greater than 0 if x > y, less than 0 if x < y |
++x |
Pre-increment | Increments x by one, then returns x |
x++ |
Post-increment | Returns x, then increments x by one |
--x |
Pre-decrement | Decrements x by one, then returns x |
x-- |
Post-decrement | Returns x, then decrements x by one |
x and y |
And |
true if both x and y are true . If x=6, y=3 then(x < 10 and y > 1) returns true
|
x && y |
And |
true if both x and y are true . If x=6, y=3 then(x < 10 && y > 1) returns true
|
x or y |
Or |
true if any of x or y are true . If x=6, y=3 then(x < 10 or y > 10) returns true
|
x || y |
Or |
true if any of x or y are true . If x=6, y=3 then(x < 3 || y > 1) returns true
|
a . b |
Concatenation | Concatenate two strings: "Hi" . "Ha" |
![Gordon](https://i.stack.imgur.com/LXDBV.png?s=256&g=1)
Gordon
I'm a Development Program Director, IBM Observability by Instana. Previously an Engineering Manager and Software Engineer. Retired Stack Overflow Moderator
Updated on July 08, 2022Comments
-
Gordon almost 2 years
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 for 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)- Nullable return type declaration
- Nullable parameter type declaration
- php method argument type hinting with question mark (?type)
:
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 <>
- 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
?->
NullSafe Operator Calls (since PHP 8.0)
-
Mike almost 13 yearsI know this isn't strictly PHP, but what about including a link to phpdoc.org for phpDocumentor comment syntax, which is commonly used and it's also impossible to search for
/**
? -
ajreal over 12 yearsCan I suggest square bracket and curly bracket ?
-
dncrane over 12 yearsI ran into this problem a lot too (not being able to search for special characters), which is why I made SymbolHound, a search engine that doesn't ignore special characters. I also posted it on StackApps.
-
Herbert almost 12 yearsWell, from the heading Why is this?, I'd guess it's because "The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them".
-
Josh almost 10 yearsAmazing post. Even as an senior php developer this is very handy to reference at times when I need to verbally explain things to my team.
-
Martin Ender over 9 yearsShould we add this one? It's not syntax, but similarly hard to google, and may look like syntax to some people.
-
Funk Forty Niner over 8 yearsA question was asked today (Nov.20/15) stackoverflow.com/questions/33833259/what-is-rscat-in-php asking "What is $rsCat in php" (should that question still be made visible and not deleted). Strangely enough, there isn't a reference about
$
variable, but about$$
Variable Variables only. I believe that should be amended somewhere. -
Typel over 8 yearsI came across this one the other day:
|=
-
Gordon over 8 years@Typeless did you mean stackoverflow.com/questions/3362626/…?
-
Typel over 8 years@Gordon. No, I actually saw it in a popular PasswordHash class. The full line looks like this:
$diff |= ord($a[$i]) ^ ord($b[$i]);
-
Deduplicator over 8 years@davejal: Please make sure a tag applies instead of simply blindly adding it.
-
mplungjan almost 7 years@dncrane - I could not search for
... javascript
(spread/rest) rainsoft.io/how-three-dots-changed-javascript -
Funk Forty Niner almost 7 years@Gordon I recently closed this question and wanted to use this Q&A to close it with, however the
?
query string character isn't included here. I closed the question with two other ones instead that directly address it. Do you feel that it should be added in this Q&A here? -
Funk Forty Niner almost 7 years@Gordon The "... Argument unpacking (since PHP 5.6)" hyperlink for wiki.php.net/rfc/argument_unpacking produces as "502 Bad Gateway" for me today on a few browsers. You may want to look into that and am unsure if you saw my previous comment just above this one a few days prior. I saw this question today and was going to close the question with this one, but cannot since the link isn't valid right now.
-
Gromski almost 7 years@Fred-ii- 1. Let's stick to PHP syntax only,
?
in a URL is unrelated. 2. This is a community wiki, edit/add as needed. -
Funk Forty Niner almost 7 years@deceze Oh I see. Ok well, if it's unrelated then it (I) shouldn't add to it then, correct? Edit: I see you fixed the reference for the
...
link. -
Mahmoud Zalt over 6 yearsHere's the names of all the PHP operators (TOKEN). Reference php.net/manual/en/tokens.php ![enter image description here](i.stack.imgur.com/P5ilY.jpg)
-
Lotus Notes over 13 yearsFor everyone's sake, please remove the bolded information about pre-incrementing being infinitesimally faster. This is the absolute worst example of premature optimization and this kind of information should not be in people's heads if they are just starting to learn PHP.
-
Peter Ajtai over 13 years@Lotus - I consider it a fun fact. If you're a beginner to PHP, or C++, etc, it seems pretty wacky that ++i and i++ are different enough to work at different speeds. I found it fascinating.
-
Lotus Notes over 13 years@Peter Ajtai Yes, it's interesting, but from the way you've structured your post you make it seem like one of the prime facts of PHP that is absolutely vital to using the language.
-
Aycan Yaşıt almost 11 yearsWhat if $a takes a value greater than 255?
-
Ryan Vincent over 10 yearsThe above works with 'interfaces' as well. This is useful for checking that a particular interface is available.
-
rev almost 9 yearsNot sure how
$a <=> $b
is different to$a - $b
-
mcrumley almost 9 years@AcidShout
$a - $b
works for numbers, but not strings, objects, or arrays. -
Mark Amery over 8 years@mcrumley No, it's worse than that. In general
$a - $b
doesn't even work for numbers; it works only for integers. It doesn't work for non-integer numbers, becauseusort
casts your comparator function's return values toint
, which means 0.5 gets cast to 0, which means that two numbers with a difference of less than 1, such as 4 and 4.6, may (depending upon which one gets passed as the first argument to your comparator function) incorrectly compare as equal. -
Mark Amery over 8 yearsSherif, why state that the operator returns "less than 0", 0 or "more than 0" rather than simply stating that it returns 1, 0, or -1? The latter is both more precise and more succinct; I don't understand why you reverted that detail of my edit.
-
Alma Do over 8 yearsBtw, prior to PHP7 there's still a way to avoid if-else block that works not only with numbers:
return $a < $b ? -1 : $a != $b;
-
Sherif over 8 years@MarkAmery Because that is the documented behavior.
-
Mark Amery over 8 years@Sherif php.net/manual/en/migration70.new-features.php documents it as
1
/0
/-1
, not to mention that the original RFC did likewise. I think that's sufficient documentation to justify including the same detail here, and have rolled back for now; feel free to rollback and continue this argument if you still disagree. -
Sherif over 8 years@MarkAmery the migration guide isn't the documented behavior of the operator. For that you want to look at the language operators section of the manual for that php.net/language.operators.comparison the actual behavior behind this relies on various comparison functions of the API, like when you're doing strcmp for strings, where you can not guarantee the actual return value in every single case. Sure, it's almost always 1, 0, or -1, but for the cases where you can't guarantee it like in wrapping libc's strcmp, you offer up the same defined behavior as the underlying spec to be safe
-
Sherif over 8 years@MarkAmery The point here is not to allow people to rely on undefined behavior. For the one case where someones gets a value that is not exactly 1, 0, or -1, you get someone filing a bug report thinking there's something wrong in the language. Which is why we document that all we can guarantee is that value will be less than, greater than, or equal to 0, and not necessarily 1, 0, and -1.
-
AlbertSamuel about 8 years@AycanYaşıt Most of the operating system is using 32 bit and 64 bit system, that means the limit is much more than 255 (8 bits).
-
Andrei almost 7 years{''} notation for property names. echo $movies->movie->{'great-lines'}->line; can be used to access data in SimpleXMLElement. Maybe it can be used in other cases too. php.net/manual/en/simplexml.examples-basic.php
-
PHPFan over 6 years@Rizier123 : What does mean by "Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables."?
-
Kaii about 6 years@AycanYaşıt Actually, the representation here with one byte length isn't even correct, as even the smallest integer is still 64 bit (8 byte) in memory on a modern 64bit platform.
-
G_real over 5 yearsWhy
and &
is0 0 0 0 1 0 0 0
andor |
is0 0 0 0 1 0 1 1
? -
stramin almost 5 yearsThere is an error in
$a ?? $c
, it says is the same as$a ? $a : $c
, but ternary operator checks if value is true, on the other hand, null coalescing checks for null values, so, if $a is 0, you will get 0 (because 0 is not null), for example if you have:$a=0; $c=5;
then$a?$a:$c
returns 5, and$a??$c
returns 0. -
IMSoP about 4 yearsAdding this feels rather premature, nobody will see this operator in any live code, and it might not even be added. It would be better to wait until the details are finalised and document it properly.
-
IMSoP about 4 yearsAdding this feels rather premature, nobody will see this operator in any live code, and it might not even be added. It would be better to wait until the details are finalised and document it properly.
-
John Conde about 4 yearsWe are already getting questions on it. :/ If the proposal fails we can then decide if this is worth keeping.
-
IMSoP about 4 years@JohnConde I'd be tempted to close them as off-topic, because they're asking about an imaginary programming language; or perhaps "this question belongs on a different part of the timeline, please check the settings on your time machine" ;)
-
Alberto Sinigaglia about 4 years@IMSoP i'm with you, however i was invited to do so here stackoverflow.com/questions/62178354/… ...
-
John Conde about 4 yearslol The thought crossed my mind. I asked myself if this would offer value to future visitors and, for now, the answer is "yes". But that may change...
-
IMSoP about 4 years@JohnConde The problem is that we can't actually give a correct answer yet - the actual behaviour of the operator might completely change, or it might never be added, so anything we write now is as likely to mislead as inform. The only honest answer is "a syntax error, but there's a possibility it will mean something in the future".
-
IMSoP about 4 yearsIt looks like we now have two answers about this. Personally, I would just have voted to close the other question rather than trying to answer, because right now there is no such operator.
-
John Conde about 4 yearsI can't say I disagree with your point of view. I'm okay with that as an answer.
-
Alberto Sinigaglia about 4 years@IMSoP there are a lot of questions here on StackOverflow about C++ Proposal, and they are not been closed: despite that, i'm with you about not posting here this operator for the moment
-
IMSoP about 4 yearsI don't know much about the C++ process and when it's reasonable to discuss proposals, and to be fair this one seems fairly likely to succeed workout modification, but in general there are a lot of features proposed for PHP that never make it to the language, and it would be rather confusing for users to come across them all in reference lists. That said, I'm partly just being a grumpy old man here :P
-
Alberto Sinigaglia about 4 yearsAhah @IMSoP i'm a relative newby here, i code only in the next 6 years, so I trust you, if you tell me it's not his place, I'll take it off
-
SherylHohman about 4 yearsAny idea what the double underscore function is?
__()
. It is used in WordPress. I cannot find a definition for it anywhere. Example of its use: core.trac.wordpress.org/browser/tags/5.4/src/wp-admin/includes/…. It seems that to use the WPimage_size_names_choose()
filter,__()
must be used to add the "short name" to the array of named_sizes you want to add. -
Mohammed H almost 4 years@SherylHohman Look like it is translation function. developer.wordpress.org/reference/functions/__
-
SherylHohman almost 4 yearsThanks so much! I envy your superior Google skills (and vast coding knowledge) :-)
-
Umair Khan almost 4 years@JohnConde. The proposal of Nullsafe operator is accepted and implemented in PHP 8. Kindly update your answer. Also update RFC link.
-
Umair Khan almost 4 years@Berto99 The proposal of Nullsafe operator is accepted and implemented in PHP 8. Kindly update your answer. Also update RFC link.
-
mickmackusa over 3 years[TODO] Include new functionality in PHP8 to use the splat operator with "named parameters" ref: stackoverflow.com/a/64997399/2943403
-
bytepan almost 3 yearsreturn $b <=> $a; // for reversing order
-
Sherif almost 3 years@bytepan right you are. I updated the comment 😉