The Triple Equals in PHP
Posted by Jon Lee in Web Development, tags: coding, operators, PHP, triple-equals, web developmentAlthough I consider myself a fairly seasoned programmer, I am actually relatively new to PHP. I have never had any formal training in PHP nor have I even read a book about it. Most of what I have learned is strictly from studying other code and good old-fashioned trial and error.
In over 15 years of programming experience, last week was the first time I saw the triple equals operator “===”. Let’s start with the single equals operator and build our way to the triple.
Single Equals
A single equal sign in PHP is an assignment operator. It assigns the value of the right side to the variable on the left side. For example:
$name = “jon”;
echo $name; //output is “jon”
Double Equals
Two equal signs in PHP is a comparison operator used to check if the value on the left is the same as the value on the right. This is what you normally use in If statements. For example:
$a = 5;
$b = 4+1;
// then $a == $b is true
Triple Equals
Three equal signs in PHP is also a comparison operator but looks not only at the value but also looks at the type of the variables. For example:
$age1 = “5″;
$age2 = 5;
// then $age1 == $age2 is true
// and $age1 === $age2 is false because the variables are of different types
I don’t know of any other language that uses the triple equals operator (it was only introduced in PHP4 after all) but many other languages use “=” and “==” in the same way as PHP.
Popularity: 7% [?]
Entries (RSS)