Operators in PHP

O
Strings in PHP
Math functions in PHP

Operators are used for performing operations between variables or values. Almost all languages provide a wide array of operators for mathematical values, strings, arrays and more.
PHP 8 is loaded with useful operators.

Below are the different kinds of operators that PHP 8 has to offer us.

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

Arithmetic operators

Arithmetic operators perform a calculation between two or more numeric values. Below we will show these operators and use them in combination with 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.

You can practice with the above operators with two or more variables.

One of the operators mentioned above which is not that straight forward is the modulo operator. Below some examples of this operator in action.

<?php

echo (7 % 3); // output 1
echo (7 % -3); // outputs 1
echo (-7 % 3); // outputs -1
echo (-7 % -3); // outputs -1

?>

Assignment Operators

Assignment operators are used for assigning a value to a 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

You can also assign a value by reference using assignment operators. 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

Comparison operators are 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.

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 Operators

Increment and decrement operators increase or decrease a value by one respectively. They only work with numbers and strings. Increment/decrement operators do not work with boolean variables and nor with arrays.

pre-increment and post-increment

Pre-increment

Increments $x by one and then returns $x.

<?php

$x=5;
echo ++$x; // outputs 6

?>

Post-increment

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

<?php

$x=5;
echo $x++; // outputs 5
echo $x; // outputs 6

?>

pre-decrement and post-decrement in PHP

pre-decrement

Decrements $x by one, then returns $x.

<?php

$x=5;
echo --$x; // outputs 4

?>

Post-decrement

Returns $x, then decrements $x by one.

<?php

$x=5;
echo $x--; // outputs 4

?>

Logical Operators

Logical operators output either true or false. The following table illustrates these operators.

$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.

String Operators

There are two string operators in PHP.

  • Concatenation Operators ( . )
  • Concatenation Assignment Operators ( . )

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 the 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.

Two arrays are equal if they have the 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 three conditional assignment operators in PHP: && the logical and operator || the logical OR operator and ?: the ternary operator.

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

Similar to ternary but this operator checks for NULL

<?php
$x = $var1 ?? $var2 : $var3;

// The value of $x will be assigned to $var2 
// if $var1 is NULL, else it will be assigned $var3
?>

Other than these operators discussed above we have the` operator known as an execution operator and it helps in running the shell commands.

In case you want to research operators in PHP more you can check out the official reference to PHP 8 operators.

Strings in PHP
Math functions in PHP

Tutor Network

Share this

Learn PHP from A to Z