Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | $x + $y | Sum of $x and $y |
- |
Subtraction | $x - $y | Difference of $x and $y |
* |
Multiplication | $x * $y | Product of $x and $y |
/ |
Division | $x / $y | Quotient of $x and $y |
% |
Modulus | $x % $y | Remainder of $x divided by $y |
** |
Exponentiation | $x ** $y | $x to the power of $y |
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
Operator | Description | Example | Result |
---|---|---|---|
= |
Assignment | $x = $y | Assign $y to $x |
+= |
Addition Assignment | $x = $x + $y | Add $y to $x and assign the result to $x |
-= |
Subtraction Assignment | $x = $x - $y | Subtract $y from $x and assign the result to $x |
*= |
Multiplication Assignment | $x = $x * $y | Multiply $x by $y and assign the result to $x |
/= |
Division Assignment | $x = $x / $y | Divide $x by $y and assign the result to $x |
%= |
Modulus Assignment | $x = $x % $y | Modulus of $x divided by $y and assign the result to $x |
Avoiding == Versus = Confusion
Use:
if (12 == $dwarves) { ... }
if ($dwarves == 12) { ... }
if (12 = $dwarves) { ... }
if ($dwarves = 12) { ... }
Putting a constant on the left side of a comparison coerces the comparison to the type of the constant. This causes problems when you are comparing an integer with a variable that could be an integer or a string. 0 == $dwarves is true when $dwarves is 0, but it’s also true when $dwarves is sleepy. Because an integer (0) is on the left side of the comparison, PHP converts what’s on the right (the string sleepy) to an integer (0) before comparing. To avoid this, use the identity operator, 0 === $dwarves, instead.
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | $x == $y | Returns true if $x is equal to $y |
=== |
Identical to | $x === $y | Returns true if $x is equal to $y, and they are of the same type |
!= |
Not equal to | $x != $y | Returns true if $x is not equal to $y |
<> |
Not equal to | $x <> $y | Returns true if $x is not equal to $y |
!== |
Not identical to | $x !== $y | Returns true if $x is not equal to $y, or they are not of the same type |
< |
Less than | $x < $y | Returns true if $x is less than $y |
<= |
Less than or equal to | $x <= $y | Returns true if $x is less than or equal to $y |
> |
Greater than | $x > $y | Returns true if $x is greater than $y |
>= |
Greater than or equal to | $x >= $y | Returns true if $x is greater than or equal to $y |
<=> |
Spaceship operator | $x <=> $y | Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7. |
The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP's usual type comparison rules.
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
PHP Increment / Decrement Operators
The PHP increment and decrement operators are used to increase or decrease the value of a variable.
Operator | Description | Result |
---|---|---|
++$x |
Pre-increment | Increments $x by one, then returns $x |
$x++ |
Post-increment | Returns $x, then increments $x by one |
--$x |
Pre-decrement | Decrements $x by one, then returns $x |
$x-- |
Post-decrement | Returns $x, then decrements $x by one |
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
Operator | Description | Example | Result |
---|---|---|---|
&& |
Logical AND | $x && $y | Returns true if both $x and $y are true |
|| |
Logical OR | $x || $y |
Returns true if either $x or $y is true |
! |
Logical NOT | ! $x | Returns the opposite of $x |
and |
Logical AND | $x and $y | Returns true if both $x and $y are true |
or |
Logical OR | $x or $y | Returns true if either $x or $y is true |
xor |
Logical Xor | $x xor $y | True if either $x or $y is true, but not both |
PHP String Operators
The PHP string operators are used to concatenate strings.
Operator | Description | Example | Result |
---|---|---|---|
. |
Concatenation | $x . $y | Concatenates $x and $y |
.= |
Concatenation assignment | $x .= $y | Appends $y to $x |
PHP Array Operators
The PHP array operators are used to perform operations on arrays.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Union | $x + $y | Union of $x and $y |
== |
Equality | $x == $y | Returns true if $x and $y have the same key/value pairs |
=== |
Identity | $x === $y | Returns true if $x and $y have the same key/value pairs in the same order and of the same types |
!= |
Inequality | $x != $y | Returns true if $x is not equal to $y |
<> |
Inequality | $x <> $y | Returns true if $x is not equal to $y |
!== |
Non-identity | $x !== $y | Returns true if $x is not identical to $y |
Conditional Assignment Operators
Operator | Description | Example | Result |
---|---|---|---|
?: |
Ternary Operator | $x = expr1 ? expr2 : expr3 | Returns the value of $x. The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE |
?? |
Null coalescing | $x = expr1 ?? expr2 | Returns the value of $x. The value of $x is expr1 if expr1 exists, and is not NULL. If expr1 does not exist, or is NULL, the value of $x is expr2. Introduced in PHP 7 |
Converting an if
statement to a Ternary Operator
A ternary operator is a shorthand for if-else
statements in PHP. The syntax is:
condition ? value_if_true : value_if_false;
Example: Standard if
statement
$age = 20;
if ($age >= 18) {
$status = "Adult";
} else {
$status = "Minor";
}
Equivalent Ternary Operator
$age = 20;
$status = ($age >= 18) ? "Adult" : "Minor";
Null Coalescing Operator (??
)
The null coalescing operator (??
) is used to check if a variable is set and not null
. If the variable is set, it returns its value; otherwise, it returns a default value.
It was added as a syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not null; otherwise it returns its second operand.
Example: Using isset()
// Fetches the value of $_GET['user'] and returns 'Guest'
// if it does not exist.
$username = isset($_GET['user']) ? $_GET['user'] : 'Guest';
Equivalent with Null Coalescing Operator (??
)
$username = $_GET['user'] ?? 'Guest';
isset()
. It ensures that if $_GET['user']
is not set or is null
, the default value 'Guest'
is used.
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'Guest'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'Guest';