Difference between == and === in PHP ?

In this blog, we will discuss the differences between ‘==’ and ‘===’ operators in PHP. Both are comparison operators used to compare two or more values.

== Operator: This operator is used to check whether the given values are equal or not. If yes, it returns true, otherwise, it returns false.

Syntax : operand1_value == operand2_value

=== Operator: This operator is used to check the given values and their data type are equal or not. If yes, then it returns true, otherwise, it returns false.

Syntax : operand1_value === operand2_value

Here, the main point to remember is that the === operator will return false when the data types of the operands are different.

Example 1: The following code will show the example of == operators with the same and different data type operands.

<?php

$x = 10;
$y = 10;

if ($x == $y) {
    echo "Variables are Equal";
} else {
    echo "Not Equal";
}

echo "\n";

// Show a message if two operands are equal with different data type operands
// First is of string type and the second is of integer type

if ("10" == 10) {
    echo "Variables are Equal";
} else {
    echo "Not Equal";
}

?>

OUTPUT :

Variables are Equal

Variables are Equal

Example 2: The following code will show the example of === operators with the same and different data type operands.

<?php

$x = 10;
$y = 10;

// Return a message if two operands are equal with same data type operands
if($x === $y){
	echo "Variales are Equal";
}
else{
	echo "Not Equal";
}

echo "\n";

// Return a message if two operands are equal
// with different data type operands
// First is of string type and the second
// is if integer type
if('10' === 10){
	echo "Variales are Equal";
}
else{
	echo "Not Equal";
}

?>

OUTPUT :

Variables are Equal

Not Equal

So, thanks for following this blog, and if you need any help please leave your queries in the comment box we will be happy to help you.

Difference between == and === in PHP ?expoundsofttechsolution d