PHP Operators

This section demonstrates various operators that can be used in PHP scripts.

PHP Arithmetic Operators

Operator Name Example Result Display Result
+ Addition $x + $y The sum of $x and $y Display Result
- Subtraction $x - $y The difference between $x and $y Display Result
* Multiplication $x * $y The product of $x and $y Display Result
/ Division $x / $y The quotient of $x and $y Display Result
% Modulus $x % $y The remainder of $x divided by $y Display Result

The following example demonstrates the different results of using different arithmetic operators:

Example

<?php 
$x=17; 
$y=8;
echo ($x + $y); // Outputs 25
echo ($x - $y); // Outputs 9
echo ($x * $y); // Outputs 136
echo ($x / $y); // Outputs 2.125
echo ($x % $y); // Outputs 1
?>

Run Example

PHP Assignment Operators

PHP Assignment Operators are used to write values to variables.

The basic assignment operator in PHP is "=". This means that the right-hand assignment expression will set the value of the left-hand operand.

Assignment Is equivalent to Description Display Result
x = y x = y The right-hand expression sets the value of the left-hand operand. Display Result
x += y x = x + y Addition Display Result
x -= y x = x - y Subtraction Display Result
x *= y x = x * y Multiplication Display Result
x /= y x = x / y Division Display Result
x %= y x = x % y Modulus Display Result

The following example demonstrates the different results of using different assignment operators:

Example

<?php 
$x=17; 
echo $x; // Outputs 17
$y=17; 
$y += 8;
echo $y; // Outputs 25
$z=17;
$z -= 8;
echo $z; // Outputs 9
$i=17;
$i *= 8;
echo $i; // Outputs 136
$j=17;
$j /= 8;
echo $j; // Outputs 2.125
$k=17;
$k %= 8;
echo $k; // Outputs 1
?>

Run Example

PHP String Operators

Operator Name Example Result Display Result
. Concatenation $txt1 = "Hello" $txt2 = $txt1 . " world!" Now $txt2 contains "Hello world!" Display Result
.= Concatenation Assignment $txt1 = "Hello" $txt1 .= " world!" Now $txt1 contains "Hello world!" Display Result

The following example demonstrates the result of using string operators:

Example

<?php
$a = "Hello";
$b = $a . " world!";
echo $b; // Output Hello world!
$x="Hello";
$x .= " world!";
echo $x; // Output Hello world!
?>

Run Example

PHP Increment/Decrement Operators

Operator Name Description Display Result
++$x Pre-increment Increment $x by one, then return $x Display Result
$x++ Post-increment Return $x, then increment $x by one Display Result
--$x Pre-decrement Decrement $x by one, then return $x Display Result
$x-- Post-decrement Return $x, then decrement $x by one Display Result

The following example shows different results of using different increment/decrement operators:

Example

<?php
$x=17; 
echo ++$x; // Output 18
$y=17; 
echo $y++; // Output 17
$z=17;
echo --$z; // Output 16
$i=17;
echo $i--; // Output 17
?>

Run Example

PHP Comparison Operators

PHP comparison operators are used to compare two values (numbers or strings):

Operator Name Example Result Display Result
== Equal $x == $y If $x is equal to $y, then return true. Display Result
=== Equal (completely the same) $x === $y If $x is equal to $y and their types are the same, then return true. Display Result
!= Not equal $x != $y Returns true if $x is not equal to $y. Display Result
<> Not equal $x <> $y Returns true if $x is not equal to $y. Display Result
!== Not equal (completely different) $x !== $y If $x is not equal to $y, or their types are different, then return true. Display Result
> Greater than $x > $y If $x is greater than $y, then return true. Display Result
< Less than $x < $y If $x is less than $y, then return true. Display Result
>= Greater than or equal to $x >= $y If $x is greater than or equal to $y, then return true. Display Result
<= Less than or equal to $x <= $y If $x is less than or equal to $y, then return true. Display Result

The following example shows different results of using certain comparison operators:

Example

<?php
$x=17; 
$y="17";
var_dump($x == $y);
echo "<br>";
var_dump($x === $y);
echo "<br>";
var_dump($x != $y);
echo "<br>";
var_dump($x !== $y);
echo "<br>";
$a=17;
$b=8;
var_dump($a > $b);
echo "<br>";
var_dump($a < $b);
?>

Run Example

PHP Logical Operators

Operator Name Example Result Display Result
and And $x and $y If $x and $y are both true, then return true. Display Result
or Or $x or $y If $x and $y are both true or at least one of them is true, then return true. Display Result
xor XOR $x xor $y If $x and $y are true and only one of them is true, then return true. Display Result
&& And $x && $y If $x and $y are both true, then return true. Display Result
|| Or $x || $y If $x and $y are both true or at least one of them is true, then return true. Display Result
! Not !$x Returns true if $x is not true. Display Result

PHP Array Operators

PHP array operators are used to compare arrays:

Operator Name Example Result Display Result
+ Union $x + $y Union of $x and $y (but does not overwrite duplicate keys) Display Result
== Equal $x == $y Returns true if $x and $y have the same key/value pairs. Display Result
=== Identical $x === $y Returns true if $x and $y have the same key/value pairs and the same order and type. Display Result
!= Not Equal $x != $y Returns true if $x is not equal to $y. Display Result
<> Not Equal $x <> $y Returns true if $x is not equal to $y. Display Result
!== Not Equal $x !== $y Returns true if $x and $y are completely different. Display Result

The following example demonstrates the different results of using different array operators:

Example

<?php
$x = array("a" => "apple", "b" => "banana"); 
$y = array("c" => "orange", "d" => "peach"); 
$z = $x + $y; // Union of $x and $y
var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>

Run Example