Double and Triple equals operator in PHP

Have you been given a piece of code to maintain that has single, double and triple equal operators in it? What’s the difference?

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

Permanent link to this article:

Leave a Reply

Your email address will not be published.