Operator Precedence
| Associativity |
Operators |
Additional Information |
| non-associative |
new |
new |
| left |
[ |
array() |
| non-associative |
++ -- |
increment/decrement |
| non-associative |
! ~ - (int) (float) (string) (array) (object) @ |
types |
| left |
* / % |
arithmetic |
| left |
+ - . |
arithmetic and string |
| left |
<< >> |
bitwise |
| non-associative |
< <= > >= |
comparison |
| non-associative |
== != === !== |
comparison |
| left |
& |
bitwise and references |
| left |
^ |
bitwise |
| left |
| |
bitwise |
| left |
&& |
logical |
| left |
|| |
logical |
| left |
? : |
ternary |
| right |
= += -= *= /= .= %= &= |= ^= <<= >>= |
assignment |
| left |
and |
logical |
| left |
xor |
logical |
| left |
or |
logical |
| left |
, |
many uses |
Left associativity means that the expression is evaluated from left
to right, right associativity means the opposite.
Arithmetic Operators
Remember basic arithmetic from school? These work just like those.
Arithmetic Operators
| Example |
Name |
Result |
| -$a |
Negation |
Opposite of $a. |
| $a + $b |
Addition |
Sum of $a and $b. |
| $a - $b |
Subtraction |
Difference of $a and $b. |
| $a * $b |
Multiplication |
Product of $a and $b. |
| $a / $b |
Division |
Quotient of $a and $b. |
| $a % $b |
Modulus |
Remainder of $a divided by $b. |
The division operator ("/") returns a float value anytime, even
if the two operands are integers (or strings that get converted to
integers).
Note: Remainder $a % $b is negative
for negative $a.
Assignment Operators
The basic assignment operator is "=". Used it like $a=5;
In addition to the basic assignment operator, there are "combined
operators" for all of the binary arithmetic and string operators
that allow you to use a value in an expression and then set its
value to the result of that expression. For example:
<?php
$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!",
just like $b = $b . "There!";
?>
Bitwise Operators
| Example |
Name |
Result |
| $a & $b |
And |
Bits that are set in both $a and $b are set. |
| $a | $b |
Or |
Bits that are set in either $a or $b are set. |
| $a ^ $b |
Xor |
Bits that are set in $a or $b but not both are set. |
| ~ $a |
Not |
Bits that are set in $a are not set, and vice versa. |
| $a << $b |
Shift left |
Shift the bits of $a $b steps to the left (each step means "multiply
by two") |
| $a >> $b |
Shift right |
Shift the bits of $a $b steps to the right (each step means "divide
by two") |
Comparison Operators
| Example |
Name |
Result |
| $a == $b |
Equal |
TRUE if $a is equal to $b. |
| $a === $b |
Identical |
TRUE if $a is equal to $b,
and they are of the same type. (introduced in PHP 4) |
| $a != $b |
Not equal |
TRUE if $a is not equal
to $b. |
| $a <> $b |
Not equal |
TRUE if $a is not equal
to $b. |
| $a !== $b |
Not identical |
TRUE if $a is not equal
to $b, or they are not of the same type. (introduced in PHP
4) |
| $a < $b |
Less than |
TRUE if $a is strictly less
than $b. |
| $a > $b |
Greater than |
TRUE if $a is strictly greater
than $b. |
| $a <= $b |
Less than or equal to |
TRUE if $a is less than
or equal to $b. |
| $a >= $b |
Greater than or equal to |
TRUE if $a is greater than
or equal to $b. |
If you compare an integer with a string, the string is converted to
a number. If you compare two numerical strings, they are compared
as integers. These rules also apply to the switch statement.
|