Double and Triple equals operator in PHP

  • A double = sign is a comparison and tests whether the variable / expression / constant to the left has the same value as the variable / expression / constant to the right.
  • A triple = sign is a comparison to see whether two variables / expressions / constants are equal AND have the same type – i.e. both are strings or both are integers.

In simplest terms:

[pastacode lang=”php” manual=”%3D%3D%20checks%20if%20equivalent%20(value%20only)%0A%0A%3D%3D%3D%20checks%20if%20the%20same%20(value%20%26%26%20type)%20%0A” message=”PHP code” highlight=”” provider=”manual”/]

In PHP:

true == 1 (true – equivalent in value)

true === 1 (false – not the same in value && type)

true is boolean

1 is int

Here’s a table we put together showing how some variables compare to each other.

// “===” means that they are identical

// “==” means that they are equal

// “!=” means that they aren’t equal.

[ad type=”banner”]

Difference between == and ===

Comparison Operators:-

Loosely == equal comparison:

  • If you are using the == operator, or any other comparison operator which uses loosely comparison such as !=, <> or ==, you always have to look at the context to see what, where and why something gets converted to understand what is going on.

Strict === identical comparison:

  • If you are using the === operator, or any other comparison operator which uses strict comparison such as !== or ===, then you can always be sure that the types won’t magically change, because there will be no converting going on.
  • So with strict comparison the type and value have to be the same, not only the value.

Example:

[pastacode lang=”php” manual=”1%20%3D%3D%3D%201%3A%20true%0A1%20%3D%3D%201%3A%20true%0A1%20%3D%3D%3D%20%221%22%3A%20false%20%20%20%20%2F%2F%201%20is%20an%20integer%2C%20%221%22%20is%20a%20string%0A1%20%3D%3D%20%221%22%3A%20true%20%20%20%20%20%20%20%2F%2F%20%221%22%20gets%20casted%20to%20an%20integer%2C%20which%20is%201%0A%22foo%22%20%3D%3D%3D%20%22foo%22%3A%20true%20%20%20%2F%2F%20both%20operands%20are%20strings%20and%20have%20the%20same%20value%0A” message=”php code” highlight=”” provider=”manual”/]

Warning: two instances of the same class with equivalent members do NOT match the === operator. Example:

[pastacode lang=”javascript” manual=”%24a%20%3D%20new%20stdClass()%3B%0A%24a-%3Efoo%20%3D%20%22bar%22%3B%0A%24b%20%3D%20clone%20%24a%3B%0Avar_dump(%24a%20%3D%3D%3D%20%24b)%3B%20%2F%2F%20bool(false%0A” message=”javascript code” highlight=”” provider=”manual”/]

When should we use == and when ===? Here’s an example.

If we write

if ($_REQUEST[name] == “”) …..

then  testing to see if a name has been entered on a form – it will return true if no name has been entered and it will also return true if there wasn’t an input box on the form called “name” or if the URL was called up without a form at all. However, if we use the three-equals varient:

if ($_REQUEST[name] === “”) …..

then this will return true if and only if the form had an input box called “name” into which the user didn’t type anything at all.

  • Naturally, there will be times when you want to check very specifically that the form was submitted but with an empty box, and other times where you want to check simply if you’ve got a name or not (for whatever reason).
  • That’s why PHP has the flexibility and provides both == and ===
[ad type=”banner”]

Categorized in: