- Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
- Why is this?
- What should I do here?
- The List
- 25 Answers 25
- Bitwise Operator
- A few examples for better understanding
- The «AND» operator: &
- The «Or» operator: |
- Spaceship Operator
- Added in PHP 7
- Before PHP 7 you would write.
- Since PHP 7 you can write.
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
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
- 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? (&$)
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)
[] Arrays (short syntax since PHP 5.4).. Double-dot character range
I 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 /** ?
I 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.
Well, 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».
A 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.
25 Answers 25
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.
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:
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:
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: &
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: |
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.
@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).
@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.
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
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 >);
@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, because usort casts your comparator function’s return values to int , 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.
@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
@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.
The underscore character ‘_’ as in _() is an alias to the gettext() function.
Any 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 WP image_size_names_choose() filter, __() must be used to add the «short name» to the array of named_sizes you want to add.
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 pairs in 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» |
Magic constants: Although these are not just symbols but important part of this token family. There are eight magical constants that change depending on where they are used.
__LINE__ : The current line number of the file.
__FILE__ : The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__DIR__ : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__) . This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
__FUNCTION__ : The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__ : The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. Foo\Bar ). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ : The trait name. (Added in PHP 5.4.0) As of PHP 5.4 this constant returns the trait as it was declared (case-sensitive). The trait name includes the namespace it was declared in (e.g. Foo\Bar ).
__METHOD__ : The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__ : The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).