Forside > PHP > PHP Operators

PHP: Operators

By Kristoffer Bohmann

Conscise list of operators in PHP.

Page Contents:
  • String Operators
  • Array Operators
  • Type Operators
  • Arithmetic Operators
  • Assignment Operators
  • Bitwise Operators
  • Comparison Operators
  • Control Operators
  • Execution Operators
  • Incrementing/Decrementing Operators
  • Logical Operators

String Operators

Operator Example Explanation
= $a = "Hello"; Set $a to "Hello".
.= $a .= " World";
. $a = $a . " World"; Same as above.

Array Operators

Operator Example Explanation
+ $a + $b Union of $a and $b.
== $a == $b TRUE if $a and $b have same key/value pairs.
=== $a === $b TRUE if $a and $b have same key/value pairs, in the same order, and of the same types.
!= $a != $b TRUE if $a is not equal to $b.
<> $a <> $b TRUE if $a is not equal to $b (same as above).
!== $a !== $b TRUE if $a is not identical to $b (key/value pairs differ).

Type Operators (Object Type)

Operator Example Explanation
instanceof if($a instanceof SomeClass) (TRUE|FALSE)
instanceof if(!($a instanceof SomeClass)) (TRUE|FALSE)

Arithmetic Operators

Operator Example Explanation
+ $a + $b Addition.
- $a - $b
-$a
Subtraction.
Same as $a * -1.
* $a * $b Multiplication
/ $a / $b Division.
% $a % $b Modulus (negative if $b is negative).

Assignment Operators

Operator Example Explanation
= $a = "Hello"; Set string to a value.
.= $a .= " World"; Add " World" to $a.
. $a = $a . " World"; Concatenate strings.
+= $a += 2; Increment.
-= $a -= 1; Decrement.

Bitwise Operators

Operator Example Explanation
& $a & $b Bits are set in both $a and $b.
| $a | $b Or: Bits are set in both $a and $b.
^ $a ^ $b Xor: Bits are set in either $a or $b, but not both.
~ ~$a Not: Bits in $a are not set.
<< $a << $b Shift the bits of $a 2 steps to the left.
>> $a >> $b Shift the bits of $a 2 steps to the right.

Comparison Operators

Operator Example Explanation
== $a == $b TRUE if $a equal to $b.
=== a === $b TRUE if $a equal to $b and same type.
!= $a != $b TRUE if $a not equal to $b.
<> $a <> $b TRUE if $a not equal to $b (same as above).
!== $a !== $b TRUE if $a not equal to $b, or not same type.
< $a < $b TRUE if integer $a smaller than integer $b.
> $a > $b TRUE if integer $a greater than integer $b.
<= $a <= $b TRUE if integer $a smaller than or equal to integer $b.
>= $a >= $b TRUE if integer $a larger than or equal to $b.

Error Control Operators

Operator Example Explanation
@ @file("non-existing-file.html"); Ignores error messages for a PHP expression - see also error_reporting().

Execution Operators

Operator Example Explanation
`` `ls -art` Tries to execute a command in the backtick as a shell command. Same as shell_exec(). (Notice: `-backtick symbol is not '-single quote).

Incrementing/Decrementing Operators

Operator Example Explanation
++ ++$a Pre-increment.
$a++ Post-increment.
-- --$a Pre-decrement.
$a-- Post-decrement.

Logical Operators

Operator Example Explanation
and $a and $b TRUE if $a and $b are TRUE (&& has precedence over "and").
or $a or $b TRUE if $a or $b are TRUE (|| has precedence over "or").
xor $a xor $b TRUE if either $a or $b is TRUE, but not both.
! ! $a TRUE if $a is not TRUE.
&& $a && $b TRUE if $a and $b are TRUE (&& has precedence over "and").
|| $a || $b TRUE if $a or $b are TRUE (|| has precedence over "or").