What does "===" mean?

PhpOperatorsComparison OperatorsIdentity Operator

Php Problem Overview

I've noticed someone using the PHP operator === which I can't make sense out of. I've tried it with a function, and it corresponds in crazy ways.

What is the definition of this operator? I can't even find it in the declaration of PHP operators.

Php Solutions

Solution 1 - Php

> $a === $b (Identical) TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)

PHP Docs

Solution 2 - Php


> $a == $b Equal TRUE if $a is equal to $b, except for (True == -1) which is still True. > > $a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

> "5" == 5;
> "5" === 5;

Solution 3 - Php

You can read here, short summary:

> $a == $b Equal TRUE if $a is equal to $b after type juggling. > > $a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

Solution 4 - Php

In PHP you may compare two values using the == operator or === operator. The difference is this:

PHP is a dynamic, interpreted language that is not strict on data types. It means that the language itself will try to convert data types, whenever needed.

echo 4 + "2"; // output is 6

The output is integer value 6, because + is the numerical addition operator in PHP, so if you provide operands with other data types to it, PHP will first convert them to their appropriate type ("2" will be converted to 2) and then perform the operation.

If you use == as the comparison operator with two operands that might be in different data types, PHP will convert the second operand type, to the first's. So:

4 == "4" // true

PHP converts "4" to 4, and then compares the values. In this case, the result will be true.

If you use === as the comparison operator, PHP will not try to convert any data types. So if the operands' types are different, then they are NOT identical.

4 === "4" // false

Solution 5 - Php

$x == $y is TRUE if the value of the $x and $y are same:

$x = 1; //int type
$y = "1"; //string type
if ($x == $y) {
    // This will execute

$x === $y TRUE if the value of the $x and $y are same and type of $x and $y are same:

$x = 1; //int type
$y = "1"; //string type
if ($x === $y) {
    // This will not execute

Solution 6 - Php

You'll see this operator in many dynamically typed languages, not just PHP.

== will try to convert whatever it's dealing with into types that it can compare.

=== will strictly compare the type and value.

In any dynamically typed language you have to be careful with ==, you can get some interesting bugs.

The ternary === is less convenient, but it's safer. For comparisons you should always give some additional thought to whether it should be === or ==

Solution 7 - Php

> The triple equals sign === checks to see > whether two variables are equal and of the same type.

Solution 8 - Php

See Double and Triple equals operator in PHP that I got for googling on "PHP three equals operator".

At one point it says that:

> 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 / expresions / constants are equal AND have the same type - i.e. both are strings or both are integers.

It also gives an example to explain it.

Solution 9 - Php

For PHP, there many different meanings a zero can take

  1. it can be a Boolean false
  2. it could be a null value
  3. It could really be a zero

So === is added to ensure the type and the value are the same.

Solution 10 - Php

"===" matching the value in the variable as well as data type of the variable.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan KonnoView Question on Stackoverflow
Solution 1 - PhpTim SylvesterView Answer on Stackoverflow
Solution 2 - PhpDykamView Answer on Stackoverflow
Solution 3 - PhpArtem BargerView Answer on Stackoverflow
Solution 4 - PhpfarzadView Answer on Stackoverflow
Solution 5 - Phpanik4eView Answer on Stackoverflow
Solution 6 - PhpKeithView Answer on Stackoverflow
Solution 7 - PhpJonView Answer on Stackoverflow
Solution 8 - PhpumarView Answer on Stackoverflow
Solution 9 - PhpExtrakunView Answer on Stackoverflow
Solution 10 - Phpmanoj kumarView Answer on Stackoverflow