PHP Operators

I. PHP Operators

An operator takes one or more expressions and results in a value.

  • Arithmetic operators + - * / % **
  • Assignment operators = += -= *= /= %= **=
  • Comparison operators
  • Conditional operators ?: ??
  • Increment / Decrement ++ --
  • Logical operators && || ! and or xor
  • String operators . .=
  • Array operators

Note; You can check Operator Precedence here.

II. Arithmetic operators

Arithmetic operators can perform mathematical operations + - * / % **:

  • Addition.
  • Subtraction.
  • Multiplication.
  • Division.
  • Modulus.
  • Exponentiation.
$x = 8;
$y = 2;

var_dump($x + $y); // int(10)
var_dump($x - $y); // int(6)
var_dump($x * $y); // int(16)
var_dump($x / $y); // int(4)
var_dump($x % $y); // int(0)
var_dump($x ** $y); // int(64)

Tips

  • If we add + or - before a string, the value will be converted into an integer.
$x = +"8";
$y = - "2";

var_dump($x); // int(8)
var_dump($y); // int(-2)
  • The type of division result is float unless both operands are integers and are evenly divisible.
$x = 10;
$y = 3;

var_dump($x / $y); // float(3.3333333333)
  • If we want to get an infinity result when dividing 0, we use the fdiv() function.
$x = 7;
$y = 0;

var_dump(fdiv($x, $y)); // float(INF)
  • Operands of modulo are converted to int before processing.
$x = 8.5;
$y = 3.2;

var_dump($x % $y); // int(2)

III. Assignment operators

The PHP assignment operators write a value to a variable. They include = += -= *= /= %= **=.

AssignmentSimilar to
x = yx = y
x += yx = x + y
x -= yx = x - y
x *= yx = x * y
x /= yx = x / y
x %= yx = x % y
$x = 8;
$x += 2;

echo $x; // 10

IV. Comparison operators

We can use comparison operators to compare two values (number or string):

OperatorName
==Equal
===Identical
!=Not equal
<>Not equal
!==Not identical
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
<=>Spaceship
$x = 8;
$y = 2;

var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x !== $y);
var_dump($x <=> $y);

V. Conditional assignment operators

We can use conditional operators to set a value depending on conditions.

OperatorNameExample
?:Ternary$x = a ? b : c
??Null coalescing$x = a ?? b

The ternary operator returns b if a is true, c if a is false.

$x = 26;
$result = $x >= 18 ? "allow": "not allow";
echo $result; // allow

Null coalescing is mainly used when working with null. It returns b if a is null.

$x = null;
$y = $x ?? "Hello";
echo $y; // Hello

VI. Increment / Decrement operators

We can use increment and decrement operators to increase or decrease a variable’s value.

OperatorDescription
$x++Returns $x, then increments $x by one
$x–Returns $x, then decrements $x by one
++$xIncrements $x by one, then returns $x
–$xDecrements $x by one, then returns $x
$x = 10;

echo $x++; // 10
echo $x; // 11

echo ++$x; // 12;
echo --$x; // 11;

VII. Logical operators

We can use logical operators to combine conditional statements.

OperatorResult
&&True if both $x and $y are true
||True if either $x or $y is true
!True if $x is not true
andTrue if both $x and $y are true
orTrue if either $x or $y is true
xorTrue if either $x or $y is true, but not both
$x = true;
$y = false;
var_dump($x && $y); // bool(false)
var_dump($x || $y); // bool(true)
var_dump(!$x); // bool(false)

VIII. String operators

We can use string operators . .= for concatenation.

$x = "Hello";
$x .= "World";

echo $x; // HelloWorld

IX. Array operators

OperatorNameResult
+UnionUnion of $x and $y
==EqualityTrue if $x and $y have same key/value pairs
===IdentityTrue if $x and $y have the same key/value pairs same order + types
!=InequalityTrue if $x is not equal to $y
<>InequalityTrue if $x is not equal to $y
!==Non-identityTrue if $x is not identical to $y
$x = ["a" => 1, "b" => 2];
$y = ["d" => 3, "e" => 4, "f" => 5];

echo "<pre>";
print_r($x+$y);
echo "</pre>";
/*
Array
(
    [a] => 1
    [b] => 2
    [d] => 3
    [e] => 4
    [f] => 5
)
*/