This is a collection of questions that arise from time to time about PHP syntax. This is also a community wiki, so everyone is invited to participate in maintaining this list.
It used to be difficult to find questions about operators and other syntax markers. ¹
The main idea is to provide links to existing questions on Stack Overflow so that we can refer to them more easily instead of copying content from the PHP manual.
Note: As of January 2013, Stack Overflow supports special characters. Just enclose your search term in quotes, like [php]"==" vs. "==="
If you were pointed here by someone because you asked a question like this, look for the specific syntax below. The linked page of the PHP manual and the linked question may answer your question. If so, we encourage you to vote for the answer. This list is not a substitute for help from others.
If your specific token is not listed below, you may find it in the parser token list.
&
Bit operators or references
=&
References
&=
Bit operators
&&
Logical Operators
%
Arithmetic operators
!!
Logical Operators
@
Error control operator
?:
Ternary operator
Double question mark
??
Null coalescing operator (since PHP 7)
Question mark followed by type declaration
?string
?int
?array
?bool
?float
Nullable type declaration (since PHP 7.1)
?->
A question mark followed by an object operator is the NullSafe operator (since PHP 8.0)
:
Alternative syntax for control structures, ternary operators, and return type declarations
::
Range resolution operator
Namespaces
->
Classes and Objects
=>
Array
^
Bit operators
>>
Bit operators
<<
Bit operators
<<<
Heredoc or Nowdoc
=
Assignment operator
==
Comparison operators
===
Comparison operators
!==
Comparison operator
!=
Comparison operator
<>
Comparison operator
<=>
Comparison operators (since PHP 7.0)
|
Bit operators
||
Logical operators
~
Bit operators
Arithmetic operators, array operators
=
and -=
assignment operators
and --
Increment/Decrease Operators
.=
Assignment operator
.
String operators
,
Function parameters
,
Variable declaration
$$
Variable variable
`
Execution operator
<?=
Short open tag
[]
Array (short syntax since PHP 5.4)
$var = []
Empty array<?
Opening and closing tags
..
Double-dot character range
...
Parameter unpacking (since PHP 5.6)
**
Exponentiation (since PHP 5.6)
# One-line shell comment
#[]
Properties (since PHP 8)
P粉2983052662023-10-11 11:48:43
What is a point? Bits represent 1 or 0. Basically OFF(0) and ON(1)
What are bytes? A byte consists of 8 bits, and the highest value a byte can have is 255, which means every bit is set. We'll look at why the maximum value of a byte is 255.
------------------------------------------- | 1 Byte ( 8 bits ) | ------------------------------------------- |Place Value | 128| 64| 32| 16| 8| 4| 2| 1| -------------------------------------------
This means 1 byte
1 2 4 8 16 32 64 128 = 255 (1 byte)
&
$a = 9; $b = 10; echo $a & $b;
This will output the number 8. Why? Okay, let's look at an example using our table.
------------------------------------------- | 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 that the only bit they share is 8 bits.
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, and 36 is returned after addition.
|
$a = 9; $b = 10; echo $a | $b;
This will 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 columns 8, 2 and 1. Add these together: 8 2 1 = 11.
P粉3453027532023-10-11 00:19:04
Increment/Decrease Operator< /p>
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.
They can be placed before or after variables.
If placed before the variable, first increase or decrease the variable, and then return the result. If placed after a variable, first returns the variable, and then performs increment/decrement operation.
For example:
$apples = 10; for ($i = 0; $i < 10; ++$i) { echo 'I have ' . $apples-- . " apples. I just ate one.\n"; }
$i
is used in the above example because it is faster. $i
will have the same result.
Pre-increment is a bit faster because it actually increments the variable and then "returns" the result. Post-increment creates a special variable, copies the value of the first variable into it, and only after using the first variable, replaces its value with the value of the second variable.
However, you must use $apples--
because 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, the next is aa
, and so on.
Stack Overflow Post: