PHP Operators

Last Updated : May 16, 2021

In this tutorial, we will learn about how to do various operations on variables using PHP Operators. PHP Operators are a collection of symbols which represent unique operations. These operators are beneficial when we want to do the arithmetic, logic, comparison, and other operations very quickly and effectively. In PHP, there are many operators available, and it’s classified under few main categories. Please find below the supported categories in PHP,

We will learn about these operators and its detail one by one below.

Arithmetic Operators in PHP

PHP Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, etc. Please find below the operators’ list and its usages.

NameOperatorDescription
Addition+Add two variables
SubtractionSubtract two variables
Multiplication*Multiply two variables
Division/Divide one variable by another
Modulus%Calculate Quotient using two variables
Exponentiation**Do power or exponentiation using two variables

Please find below the examples using above Arithmetic Operators,

<?php
$a = 10;
$b = 5;

//addition
echo $a + $b .'<br>';

//subtraction
echo $a - $b .'<br>';

//multiplication
echo $a * $b .'<br>';

//division
echo $a / $b .'<br>';

//modulus
echo $a % $b .'<br>';

//exponentiation
echo $a ** $b .'<br>';
?>

Assignment Operators in PHP

PHP Assignment Operators are basically used to assign values to variables, it has to be done using = operator and other operators are the combination of = and an arithmetic operators. Please find below the operators’ list and its usages.

NameOperatorDescription
Assign=Assigns values to variables
Addition and Assign+=Add two variables and assign
Subtraction and assign-=Subtract two variables and assign
Multiplication and assign*=Multiply two variables and assign
Division and assign/=Divide one variable by another and assign
Modulus and assign%=Calculate Quotient using two variables and assign
Exponentiation and assign**==Do power or exponentiation using two variables and assign

Please find below the examples using the above Assignment Operators,

<?php
$a = 25;
$b = 10;

//addition and assign value to $a
echo $a += $b .'<br>';

//subtraction and assign value to $a
echo $a -= $b .'<br>';

//multiplication and assign value to $a
echo $a *= $b .'<br>';

//division and assign value to $a
echo $a /= $b .'<br>';

//do modulus and assign value to $a
echo $a %= $b .'<br>';

//do exponentiation and assign value to $a
echo $a **= $b .'<br>';
?>

Comparison Operators in PHP

PHP Comparison Operators are used to compare two values and return boolean value as an output. The values might be in the format of variables or two straight numeric or string values, etc. There are many useful operators available in this category. Please find below the operators’ list and its usages.

NameOperatorDescription
Equal==It checks two values are equal
Not Equal!=It checks two values are not equal
Not Equal<>It checks two values are not equal
Identical===It checks two values are equal with its data type
Not Identical!==It checks two values are not equal with its data type
Less than<It checks one value less than another value
Greater than>It checks one value greater than another value
Less than or Equal to<=It checks one value less than or equal to another value
Greater than or Equal to>=It checks one value greater than or equal to another value

Please find below the examples using the above Comparison Operators,

<?php
$a = 25;
$b = 10;

//checks '=='
var_dump($a == $b); echo '<br>';
var_dump(10 == 10); echo '<br>';
var_dump(10 == '10'); echo '<br>';

//checks '!='
var_dump($a != $b); echo '<br>';
var_dump(20 != 20); echo '<br>';
var_dump(20 != '20'); echo '<br>';

//checks '<>'
var_dump($a <> $b); echo '<br>';
var_dump(30 <> 30); echo '<br>';
var_dump(30 <> '30'); echo '<br>';

//checks '==='
var_dump($a === $b); echo '<br>';
var_dump(100 === 100); echo '<br>';
var_dump(100 === '100'); echo '<br>';

//checks '!=='
var_dump($a !== $b); echo '<br>';
var_dump(150 !== 150); echo '<br>';
var_dump(150 !== '150'); echo '<br>';

//checks '<'
var_dump($a < $b); echo '<br>';
var_dump(10 < 10); echo '<br>';
var_dump(15 < 10); echo '<br>';

//checks '>'
var_dump($a > $b); echo '<br>';
var_dump(10 > 10); echo '<br>';
var_dump(15 > 10); echo '<br>';

//checks '<='
var_dump($a <= $b); echo '<br>';
var_dump(10 <= 10); echo '<br>';
var_dump(15 <= 10); echo '<br>';

//checks '>='
var_dump($a >= $b); echo '<br>';
var_dump(10 >= 10); echo '<br>';
var_dump(15 >= 10); echo '<br>';
?>

Increment and Decrement Operators in PHP

PHP Increment/Decrement Operators are used to increment and decrement the variable’s value. These operators perform based on the place we will utilize in the program, like before or after on the specific variables. Also, it gets only one value because these are Unary Operators. Please find below the operators’ list and its usages.

NameOperatorDescription
Pre-Increment++ varIncrement a variable by 1 and returns the value
Post-Incrementvar ++Returns the variable then Increment the variable’s value by 1
Pre-Decrement– – varDecrement a variable by 1 and returns the value
Post-Decrementvar – –Returns the variable then Decrement the variable’s value by 1

Please find below the examples using the above Increment/Decrement Operators,

<?php
//Pre-Increment
$x = 10;
echo "Before Pre-Increment :: ". $x .'<br>';
echo "On Pre-Increment :: ". ++$x .'<br>';
echo "After Pre-Increment :: ". $x .'<br><br>';

//Post-Increment
$x = 15;
echo "Before Post-Increment :: ". $x .'<br>';
echo "On Post-Increment :: ". $x++ .'<br>';
echo "After Post-Increment :: ". $x .'<br><br>';

//Pre-Decrement
$x = 25;
echo "Before Pre-Decrement :: ". $x .'<br>';
echo "On Pre-Decrement :: ". --$x .'<br>';
echo "After Pre-Decrement :: ". $x .'<br><br>';

//Post-Decrement
$x = 35;
echo "Before Post-Decrement :: ". $x .'<br>';
echo "On Pre-Decrement :: ". $x-- .'<br>';
echo "After Post-Decrement :: ". $x;
?>

Logical Operators in PHP

PHP Logical Operators are used to compare many variable’s values and proceed based on the whole condition’s boolean value. This Logical Operators are solving individual conditions first, and with its boolean value, it will do the final validation and checks if the whole expression is true or false and returns a boolean value. Please find below the operators’ list and its usages.

NameOperatorDescription
andandIt checks two values are true
and&&It checks two values are true
ororIt checks either one value are true
or||It checks either one value are true
not!It checks the value is not true or equals to false
xorxorIt checks either one value are true but not both values

Please find below the examples using the above Logical Operators,

<?php
$a = 10;
$b = 20;

//and operator
var_dump($a and $b);
echo '<br>';

//and operator
var_dump($a && $b);
echo '<br>';
var_dump($a && $b && false);
echo '<br>';

//or operator
var_dump($a or $b);
echo '<br>';

//or operator
var_dump($a || $b);
echo '<br>';
var_dump($a || $b || false);
echo '<br>';

//not operator
var_dump(!$a);
var_dump(!$b);
var_dump(!'15');
var_dump(!15);
var_dump(!-15);
var_dump(!false);
echo '<br>';

//cor operator
var_dump($a xor $b);
echo '<br>';
var_dump($a xor false);
?>

String Operators in PHP

PHP String Operators are used to concatenate two strings. There are a bunch of functions available to manipulate Strings in PHP. Later in this tutorial series, we will learn all about them in detail. Please find below the operators’ list and its usages.

NameOperatorDescription
Concatenation.It concatenates two strings and returns output
Concatenation and assign.=It concatenates two strings and assigns output in the base variable

Please find below the examples using the above String Operators,

<?php
$first_name = "James";
$last_name = "Bond";

//concatenation
echo $first_name .' '. $last_name;
echo '<br>';

//concatenation and assignment
$first_name .=' '. $last_name;
echo $first_name;
?>

Array Operators in PHP

PHP Array Operators are used to perform various operations like merging and comparing two arrays, etc. We will learn about Array manipulation in detail in the upcoming chapter of this tutorial. Please find below the operators’ list and its usages.

NameOperatorDescription
Union+It merges two arrays
Union and assign+=It merges two arrays and assign
Equal==It checks if two arrays are the same
Not Equal!=It checks if two arrays are not the same
Not Equal<>It checks if two arrays are not the same
Indentical===It checks if two arrays are the same with their data type and order
Not Indentical!==It checks if two arrays are the same regardless of their data type and order

Please find below the examples using the above Array Operators,

<?php
//array union or merge
$a = array('One', 'Two');
$b = array('Three', 'Four');
print '<pre>'; print_r($a + $b); print '</pre>';

//array union and assign
$b +=  array('Five', 'Six');
print '<pre>'; print_r($b); print '</pre>';

//array equal
var_dump($a == $b);
echo '<br>';

//array not equal
var_dump($a != $b);
echo '<br>';
var_dump($a <> $b);
echo '<br>';

//identical
var_dump($a === $a);
echo '<br>';
var_dump($a === $b);
echo '<br>';

//not identical
var_dump($a !== $a);
echo '<br>';
var_dump($a !== $b);

?>

Spaceship Operator in PHP 7

PHP 7 has Spaceship Operator, and it is used to compare two values. Unlike Comparison Operators, it checks three conditions on the given values and returns output depending upon the satisfied condition. Please find below the details,

NameReturnsDescription
a<=>b0It checks and returns 0 if both values are equal
a<=>b1It checks and returns 1 if the left-side value is greater than the right-side value
a<=>b-1It checks and returns -1 if the right-side value is greater than the left-side value

Please find below the examples using the above Array Operators,

<?php
// numeric comparisons
echo 10 <=> 10 .'<br>';
echo 12 <=> 10 .'<br>';
echo 10 <=> 12 .'<br>';
echo 10.5 <=> 10.5 .'<br>';

// string comparisons
echo 'a' <=> 'a' .'<br>';
echo 'b' <=> 'a' .'<br>';
echo 'a' <=> 'b' .'<br>';
?>

Operators Type in PHP

In PHP, all these operators come under three types based on how many values or variables they take for doing specific operations. Please find below the list,

  • Unary Operators – This kind of operator takes only one variable or value to perform any operations like increment or decrement.
  • Binary Operators – This kind of operators takes two variables or values to perform any operations like addition, subtraction, multiplication, etc.,
  • Ternary Operators – This operator requires three variables or values to do operations. Please find below the example of this operator usage.
<?php
$a = 10;
echo ($a > 10 ? 'above 10' : 'below 10');
?>

Weightage/Precedence of the Operators in PHP

In PHP Operators, weightage or precedence determines how the operators are supposed to execute in order. Operators are executed in the expression based on their weightages. For example, division(/) has a higher weightage than addition(+), so division performed first, then PHP executes addition. Please find below the list of operators in the precedence order and their direction,

OperatorsDirection
!, ++, – –Right to Left
*, /, %Left to Right
+, –Left to Right
<, <=, >, >=Left to Right
==, !=Left to Right
&&Left to Right
||Left to Right
?:Right to Left
=, +=, -=, *=, /=, %=Right to Left

In this tutorial, we learned about PHP Operators and their usages with detailed examples. We are going to use these PHP Operators in many places to explain other concepts. We encourage you to experiment with all these operators and know their behavior to do better programming. Let’s jump into the next chapter of this tutorial series.