Introduction to PHP Operators
Operators are used for performing some operation between the variables or values. Almost all languages provide various operators to apply to mathematical values, strings, arrays, etc. Similarly, PHP 8 is loaded with useful operators. In this article, you will learn the following operators available in PHP 8.
Operators in PHP
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
Arithmetic operators in PHP
As the name suggests, arithmetic operators perform some calculation between two or more numeric values. Remember the operators you used in your school life. Here we are going to summarize them by using them with PHP variables.
|+$x||Identity||Conversion of $x to int or float as appropriate.|
|-$x||Negation||Opposite of $x.|
|$x + $y||Addition||Sum of $x and $y.|
|$x – $y||Subtraction||Difference of $x and $y.|
|$x * $y||Multiplication||Product of $x and $y.|
|$x / $y||Division||Quotient of $x and $y.|
|$x % $y||Modulo||Remainder of $x divided by $y.|
|$x ** $y||Exponentiation||Result of raising $x to the $y’th power.|
Try practicing the above operators with two and then three variables.
Tip: The modulo operator gives in the table ( % ) returns the result having the same sign as the dividend contains. For example.
<?php echo (7 % 3); // output 1 echo (7 % -3); // outputs 1 echo (-7 % 3); // outputs -1 echo (-7 % -3); // outputs -1 ?>
Assignment Operators in PHP
Assignment operators are used for assigning some value to the variable. See the following table for more detail.
|$x += $y||$z = $x + $y||Addition|
|$x -= $y||$z = $x – $y||Subtraction|
|$x *= $y||$z = $x * $y||Multiplication|
|$x /= $y||$z = $x / $y||Division|
|$x %= $y||$z = $x % $y||Modulus|
|$x **= $y||$z = $x ** $y||Exponentiation|
You can also assign value by reference using assignment operator. In this case, the actual value is not assigned, but the address of the value is assigned to the variable where its values is stored in the memory.
Comparison Operators in PHP
Comparison operators are very important for PHP developers, so we will try to explain comparison operators in this section. Comparison operators perform comparisons between the left-hand side value and right-hand-side value. The value, however, can be either numeric or non-numeric.
|$x == $y||Equal||true if $x is equal to $y after type juggling.|
|$x === $y||Identical||true if $x is equal to $y, and they are of the same type.|
|$x != $y||Not equal||true if $x is not equal to $y after type juggling.|
|$x <> $y||Not equal||true if $x is not equal to $y after type juggling.|
|$x !== $y||Not identical||true if $x is not equal to $y, or they are not of the same type.|
|$x < $y||Less than||true if $x is strictly less than $y.|
|$x > $y||Greater than||true if $x is strictly greater than $y.|
|$x <= $y||Less than or equal to||true if $x is less than or equal to $y.|
|$x >= $y||Greater than or equal to||true if $x is greater than or equal to $y.|
|$x <=> $y||Spaceship||An int less than, equal to, or greater than zero when $x is less than, equal to, or greater than $y, respectively.|
In the previous versions of PHP, if a string is compared with a numeric value or numeric string, the result is unexpected as the string is converted into a number first. This problem is now solved in PHP 8 and produces the expected output.
Increment/Decrement Operator in PHP
Increment and decrement operators increase or decrease the value by one respectively. They only work with numbers and strings. Increment/Decrement operators do not work with the boolean and array, or other data types.
There is an interesting fact, that increment operators work with NULL and add 1 in it. However, the decrement operator does not work with NULL.
What is pre increment and post increment in PHP?
Increments $x by one, then returns $x. It is written as ++$x
Returns $x, then increments $x by one. It is written as $x++
What is pre decrement and post decrement in PHP?
Decrements $x by one, then returns $x. It is written as –$x
Returns $x, then decrements $x by one. It is written as $x–
Logical Operators in PHP
We get either true or false by using logical operators. The following table illustrates the logical operators in PHP.
|$x and $y||And||true if both $x and $y are true.|
|$x or $y||Or||true if either $x or $y is true.|
|$x xor $y||Xor||true if either $x or $y is true, but not both.|
|! $x||Not||true if $x is not true.|
|$x && $y||And||true if both $x and $y are true.|
|$x || $y||Or||true if either $x or $y is true.|
There are two string operators in PHP.
- Concatenation Operator ( . )
- Concatenation Assignment Operator ( . )
The concatenation operator joins the left string with the right string. The concatenation assignment operator joins the right argument with the argument on the left side.
<?php $x = "Hey "; $y = $a . "PHP!"; // now $x contains "Hey PHP!" $x = "Hey "; $y .= "PHP!"; // now $x contains "Hey PHP!" ?>
Array operators allows to add or compare two or more arrays. PHP 8 contains following array operators.
|$x + $y||Union||Union of $x and $y.|
|$x == $y||Equality||true if $x and $y have the same key/value pairs.|
|$x === $y||Identity||true if $x and $y have the same key/value pairs in the same order and of the same types.|
|$x != $y||Inequality||true if $x is not equal to $y.|
|$x <> $y||Inequality||true if $x is not equal to $y.|
|$x !== $y||Non-identity||true if $x is not identical to $y.|
Two arrays are equal if they have same keys and values corresponding to them. However, the hard comparison === also checks for their types.
<?php $x = array("a", "b"); $y = array(1 => "a", "0" => "b"); var_dump($x == $y); // bool(true) var_dump($x === $y); // bool(false) ?>
Conditional Assignment Operators
We have two conditional assignment operators in PHP.
Ternary operator in PHP
A ternary operator is a short form of if..else statement. It returns the value based on the condition.
<?php $x = $exp1 ? $exp2 : $exp3; // The value of $x will be assigned $exp2 if the $exp1 is true, else it will be assigned $exp3 ?>
Null Coalescing Operator in PHP
Similar to ternary but checks for NULL
<?php $x = $exp1 ?? $exp2 : $exp3; // The value of $x will be assigned $exp2 if the $exp1 is NULL, else it will be assigned $exp3 ?>
Other than these major Operators in PHP, we have “ operator known as an execution operator and it helps in running the shell commands. Also, type operators such as instanceOf() check the type of the data.
For official reference to PHP 8 operators, click here.