[Explained]: What is the difference between =, == and === in PHP?
  John Mwaniki /   20 Sep 2021

[Explained]: What is the difference between =, == and === in PHP?

As a new programmer, you may have come across the usage of symbols =, == and === in programs and wondered what they are used for, or what is their differences.

In this article, you will learn in detail what these symbols are called, what they are used for, and how they differ from each other.

These symbols are referred to as operators.

In programming, an operator is a symbol that instructs the compiler or interpreter to perform a specific mathematical, relational or logical operation and produce a result.

The operators are used together with and act on operands.

An operand is an object of a mathematical or other operation that is capable of being operated on or manipulated. They are commonly expressed as constants or variables.

Example


y = x + z

From the example above, y, x and z are operands while + and = are operators.

There are several categories of operators in PHP which includes:

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

All our 3 operators for this topic happen to fall under two categories: Arithmetic operators and Comparison operators. So we will only look into these types in this article.

Assignment operators

The basic assignment operator in PHP is "=". This operator sets the left operand to the value of the expression on the right.

Example 1

<?php
$x = 4; //Assigns the value of variable $x to 4
$y = 5; // Assigns the value of variable $y to 5
echo $y; // Output: 5

$y = 2 + $x; // Assigns the value of variable $y to 2+4
echo $y; // Output: 6

$y = "Hello World!"; // Assigns the value of variable $y to a string "Hello World!"
echo $y; // Output: Hello World
?>

If you had previously assigned the value to an operand/variable, then assigned it a different value, the value changes to that of the latest assignment.

Comparison operators

The comparison operators are used to compare two values or operands.

== and === all fall under this category and are used for comparisons.

1. == Operator

It is called equal operator. It is used to check whether the operand on the left-hand and the right-hand side are equal in value or not. It returns true if they are equal in value or else returns false.

Example 1

<?php
if(5 == 5){
 echo "The values are equal";
}
else{
  echo "Values not equal";
}
?>

Returns true and output will be: The values are equal

The value 5 in both sides of the operator is equal and same type, integer.

Example 2

<?php
if(5 == "5"){
 echo "The values are equal";
}
else{
  echo "Values not equal";
}
?>

Returns true and output will be: The values are equal

The value 5 is equal on both sides. But on the left side is an integer while on the right side is a string because it is enclosed in quotes.

Example 3

<?php
if(0 == false){
 echo "The values are equal";
}
else{
  echo "Values not equal";
}
?>

Returns true and output will be: The values are equal

The value of false is 0 and of the type boolean. It returns true since the values on both sides are equal.

2. === Operator

This is called identical operator. It is used to check whether the operand on the left-hand and the right-hand side are equal in value and type or not. It returns true if they are equal in value and are of the same type or else returns false.

Example 1

<?php
if(5 === 5){
 echo "The values are equal";
}
else{
  echo "Values not identical";
}
?>

Returns true, the output is: The values are equal

The value 5 in both sides of the operator is equal and same type, integer.

Example 2

<?php
if(5 === "5"){
 echo "The values are equal";
}
else{
  echo "Values not identical";
}
?>

Returns false, the output is: Values not identical

Though the value on both sides is 5, it returns false because they are of different types. The left one is an integer while the one on the right is a string (since it is enclosed in quotes).

Example 3

<?php
if(0 === false){
 echo "The values are equal";
}
else{
  echo "Values not identical";
}
?>

Returns false, the output is: Values not identical

Though the value of false is 0, it is of type boolean and therefore not identical to the left 0 which is of type integer.

Conclusion

Though the operators =, ==, and === look closely similar, they are used differently in programming to achieve different objectives.

The = symbol is an assignment operator and is used to set the left operand to the value of the expression on the right side.

The == symbol is a comparison operator and is used to check whether the operand on the left-hand and the right-hand side are equal in value or not. It returns true if they are equal in value or else returns false.

The === symbol is a comparison operator and is used to check whether the operand on the left-hand and the right-hand side are equal in both value and type or not. It returns true if they are equal in value and are of the same type or else returns false.