The difference between ‘==’ and ‘===’
I recently spoke with a senior PHP programmer in charge of hiring programmers. She stated that some potential hires do not know the difference between ‘==’ and ‘===’ - therefore she will not even consider them. Many people miss this small detail while learning PHP but it is important to understand.The double equal will check equality. The triple equal will check equality as well, but will also check to see if the variable data types are identical.
So why would you ever want to use the ===?
Some functions, strpos for example,will return the Boolean FALSE - but also return a non-Boolean value such as 0 or an empty string (”). In this case, you would have to use ‘===’ to check the outcome of the function.
Try running the following code:
$myString=”Valerie”;
$find=”V”;
$strResult=strpos($myString,$find);
if(!($strResult)){
echo “Not Here”;
}
else{
echo “The letter “.$find.” is in the word “.$myString.”.”;
}
So although there is indeed a “V” in the string “Valerie”, the output will display “Not Here”.
Instead, we use the identical === operator:
$myString=”Valerie”;
$find=”V”;
$strResult=strpos($myString,$find);
if($strResult===false){
echo “Not Found”;
}
else{
echo “The letter “.$find.” is in the word “.$myString.”.”;
}
Filed under: PHP



I always wondered what the difference between those two was.
hi
This is a very good help.
before reading this I thought something else about “===”
Thank you
[…] thanks to Valerie at theprogrammerskit.com for the […]
Pining back from:
The Difference Between Null, Empty And Zero-Length Data / Strings