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

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Increment/Decrement operators
  5. Logical operators
  6. String operators
  7. Array operators
  8. 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.

NotationOperator NameDescription
+$xIdentityConversion of $x to int or float as appropriate.
-$xNegationOpposite of $x.
$x + $yAdditionSum of $x and $y.
$x – $ySubtractionDifference of $x and $y.
$x * $yMultiplicationProduct of $x and $y.
$x / $yDivisionQuotient of $x and $y.
$x % $yModuloRemainder of $x divided by $y.
$x ** $yExponentiationResult of raising $x to the $y’th power.
https://www.php.net/manual/en/language.operators.arithmetic.php

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.

NotationOperationOperation Name
$x += $y$z = $x + $yAddition
$x -= $y$z = $x – $ySubtraction
$x *= $y$z = $x * $yMultiplication
$x /= $y$z = $x / $yDivision
$x %= $y$z = $x % $yModulus
$x **= $y$z = $x ** $yExponentiation
https://www.php.net/manual/en/language.operators.assignment.php

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 == $yEqualtrue if $x is equal to $y after type juggling.
$x === $yIdenticaltrue if $x is equal to $y, and they are of the same type.
$x != $yNot equaltrue if $x is not equal to $y after type juggling.
$x <> $yNot equaltrue if $x is not equal to $y after type juggling.
$x !== $yNot identicaltrue if $x is not equal to $y, or they are not of the same type.
$x < $yLess thantrue if $x is strictly less than $y.
$x > $yGreater thantrue if $x is strictly greater than $y.
$x <= $yLess than or equal totrue if $x is less than or equal to $y.
$x >= $yGreater than or equal totrue if $x is greater than or equal to $y.
$x <=> $ySpaceshipAn int less than, equal to, or greater than zero when $x is less than, equal to, or greater than $y, respectively.
https://www.php.net/manual/en/language.operators.comparison.php

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?

Pre-increment

Increments $x by one, then returns $x. It is written as ++$x

Post-increment

Returns $x, then increments $x by one. It is written as $x++

What is pre decrement and post decrement in PHP?

Pre-decrement

Decrements $x by one, then returns $x. It is written as –$x

Post-decrement

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 $yAndtrue if both $x and $y are true.
$x or $yOrtrue if either $x or $y is true.
$x xor $yXortrue if either $x or $y is true, but not both.
! $xNottrue if $x is not true.
$x && $yAndtrue if both $x and $y are true.
$x || $yOrtrue if either $x or $y is true.
https://www.php.net/manual/en/language.operators.logical.php

String Operators

There are two string operators in PHP.

  1. Concatenation Operator ( . )
  2. 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

Array operators allows to add or compare two or more arrays. PHP 8 contains following array operators.

$x + $yUnionUnion of $x and $y.
$x == $yEqualitytrue if $x and $y have the same key/value pairs.
$x === $yIdentitytrue if $x and $y have the same key/value pairs in the same order and of the same types.
$x != $yInequalitytrue if $x is not equal to $y.
$x <> $yInequalitytrue if $x is not equal to $y.
$x !== $yNon-identitytrue if $x is not identical to $y.
https://www.php.net/manual/en/language.operators.array.php

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.