An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).

Comparison Operators

The PHP comparison operators are used to compare two variable’s values.

This operators are commonly used in conditional logic like if…elseif…else… operations.

Operator Name Example Result
== Equal $a == $b True if $a is equal to $b
=== Identical $a === $b True if $a is equal to $b, and they are of the same type
!= Not equal $a != $b True if $a is not equal to $b
<> Not equal $a <> $b True if $a is not equal to $b
!== Not identical $a !== $b True if $a is not equal to $b, or they are not of the same type
> Greater than $a > $b True if $a is greater than $b
< Less than $a < $b True if $a is less than $b
>= Greater than or equal to $a >= $b True if $a is greater than or equal to $b
<= Less than or equal to $a <= $b True if $a is less than or equal to $b

Example

<?php
$var_1 = 25;
$var_2 = 24;

if($var_1 == $var_2){
	echo "$var_1 and $var_2 is equal";
}

if($var_1 != $var_2){
	echo "$var_1 and $var_2 is not equal";
}

if($var_1 === $var_2){
	echo "$var_1 and $var_2 is equal and identical";
}

if($var_1 > $var_2){
	echo "$var_1 is greater than $var_2";
}

if($var_1 <= $var_2){
	echo "$var_1 is less than or eqal to $var_2";
}
?>

Logical Operators

PHP logical operators are used to test variable(s) Boolean return value.

Operator Name Example Result
and And $a and $b True if both $a and $b are true
or Or $a or $b True if either $a or $b is true
xor Xor $a xor $b True if either $a or $b is true, but not both
&& And $a && $b True if both $a and $b are true
|| Or $a || $b True if either $a or $b is true
! Not !$a True if $a is not true

Example

<?php
$var_1 = 25;
$var_2 = 24;

if($var_1 && $var_2){
echo "$var_1 and $var_2 returns true";
}

if($var_1 || $var_2){
echo "$var_1 or $var_2 returns true";
}
?>

Arithmetic Operators

Arithmetic operators are used to process mathematical operations.

Operator Name Example Output
Negation -$x Opposite of $x
+ 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

Example

<?php
$x = 4;
$y = 3;

//echo the sum of x and y
$z = $x + $y;
echo $z;

//echo the difference of x and y
$a = $x - $y;
echo $a;
?>

Array Operators

PHP array operators are used to compare arrays.

Operator Name Example Result
+ Union $a + $b Union of $a and $b (duplicate keys are not overwritten)
== Equality $a == $b True if $a and $b have the same key or value pairs
=== Identity $a === $b True if $a and $b have the same key or value pairs in the same order and of the same types
!= Inequality $a != $b True if $a is not equal to $b
<> Inequality $a <> $b True if $a is not equal to $b
!== Non-identity $a !== $b True if $a is not identical to $b

Example

<?php
$array_1 = array("name_1" => "Bryan", "name_2" => "Petter");
$array_2 = array("name_3" => "John", "name_4" => "Andrew");

if($array_1 !== $array_2){
    var_dump($array_1 + $array_2); //union two arrays
}
?>

Increment and Decrement Operators

PHP increment and decrement operators are used to add or subtract one from the variable value, respectively.

Operator Name Description
++$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

Example

<?php
$x=5;
echo ++$x; // outputs 6

$y=3;
echo $y++; // outputs 3

$z=6;
echo --$z; // outputs 5

$i=8;
echo $i--; // outputs 8
?>

String Operators

PHP string operators is used to output the multiple values assigned to a variable.

Operator Name Example
. Concatenation $var_1 = “My name”
$var_2 = $var_1 . ” is Bryan.”
.= Concatenation assignment $var = “My name”
$var .= ” is Bryan.”

Example

<?php
$var_1 = 'My name';
echo  $var_1 . ' is Bryan.'; // print My name is Bryan.

$var = 'My name';
$var .= ' is Bryan.';
echo $var; // print My name is Bryan.
?>

Assignment Operators

The PHP assignment operators is used to set a value to a variable equal to a value or another variable’s value.

Assignment of value can be done with equal “=” character.

Example

<?php
$var = 5;
$var_2 = $var;
?>

Combination of Assignment and Arithmetic Operators

Operator Example Equivalent Operation
+= $x += 5; $x = $x + 5;
-= $x -= 3; $x = $x – 3;
*= $x *= 6; $x = $x * 6;
/= $x /= 9; $x = $x / 9;
%= $x %= 2; $x = $x % 2;