Operators

Arithmetic

Binary operators

+     Addition
-     Subtraction
*     Multiplication
/     Division
%     Modulus (returns the integer remainder)

Unary operators

-     Unary negation (reverses the sign)
++    Increment (can be prefix or postfix) 
--    Decrement (can be prefix or postfix)

Assignment

=     Assign

+=    Add and assign
-=    Subtract and assign
*=    Multiply and assign
/=    Divide and assign
%=    Modulus and assign

Comparison

  • Not safe to use on strings
  • To safely compare two strings, use strcmp( )
==    Equal
!=    Not equal
>     Greater than
>=    Greater than or equal to
<     Less than
<=    Less than or equal to

<>    Not equal (alternate format)

===   Identical (equal and of the same type)
!==   Not identical

Conditional

? :   Ternary comparison operator

      condition ? val1 : val2  (Evaluates val1 if condition is true; otherwise, evaluates val2)

Boolean

  • Short-circuit logical operations
  • Evaluates the minimal number of expressions necessary
  • Partial evaluation (rather than full evaluation)
and   and
or    or
xor   xor

&&    and
||    or

!     not (logical negation)
  • Note: "and" and "or" have different precedence than "&&" and "||"

Bitwise

Binary operators

&     And  (can also be used as a boolean operator for full evaluation)
|     Or  (can also be used as a boolean operator for full evaluation)
^     Xor

<<    Shift left (zero fill) 
>>    Shift right (sign-propogating); copies of the leftmost bit (sign bit) are shifted in from the left. 

Unary operators

~     Not (inverts the bits)

String

=     Assignment
.     Concatenation
.=    Concatenate and assign

Examples

$str = "ab" . "cd";   # "abcd"
$str .= " ";          # Append a space

Array

+       # Append one array to another
+=      # Append and assign
Resources URL: 
notes/php/resources
Sources URL: 
notes/php/sources

See Also