PHP == (Equal Operator) Vs === (Identical Operator): A Difference Introduction – PHP Tutorial

By | April 14, 2020

PHP equal operator (==) and identical operator (===) are widely used in if statement, what is the difference between them. In this tutorial, we will discuss this topic for php beginners.

Equal Operator (==)

You can only compare the value of two variables.

For example:

<?php
$x = '100';
$y = 100;
if($x == $y){
	echo "they are the same";
} else {
	echo "they are not the same";
}
?>

Run this php code, you will find $x and $y are the same. Because php will convert the type of $x and $y automatically, which means $x+$y will be 200.

Identical Operator (==)

Identical Operator not only compares the value of two variables, but also will compare the type of php variable.

As to code:

<?php
$x = '100';
$y = 100;
if($x === $y){
	echo "they are the same";
} else {
	echo "they are not the same";
}
?>

$x and $y is not the same, because $x is string while $y is integer.

Category: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *