Difference Between Double and Triple Equals in PHP

Difference between double and triple equals in PHP

Difference between double and triple equals in PHP

What’s the difference between double and triple equals in PHP? PHP supports both double (==) and triple (===) comparison. Then when to use double equal to and when to use triple equal to in our code.

When I first saw the comparison using triple equal to it seems confusing in the beginning but later I understood why triple equality operator is used.

How to check memory usage by a PHP script

Difference Between Double and Triple Equals in PHP

Double equals is also known as type-converting equality. When we use double equal to in our code, it first converts the operands to the same type before comparing the values.
Let’s understand this through example –

 

 

Late static binding in PHP

Singleton design pattern in PHP

Abstract Class Vs Interface in PHP

Triple Equals (===) in PHP

Triple equals (strict comparison) compares both values as well as the data type. So let’s check what’s the output of the above script when we use triple equals.

 

Comparison using double and triple equal to in PHP

Comparison using double and triple equal to in PHP

Important Points –

PHP is a loosely typed language. Using the double equal operator allows for a loose checking of a variable.

Now let’s compare the same value with triple equal to.

To avoid such cases in your code try to use triple equals(===).

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.