57

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.

Laurel
  • 5,965
  • 14
  • 31
  • 57

10 Answers10

70
$a === $b     (Identical)      

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

PHP Docs

Kevin
  • 41,694
  • 12
  • 53
  • 70
Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
  • 3
    As a note, this equality operator also appears in Javascript and I believe Perl. It's fairly common. – Samantha Branham Jul 13 '09 at 06:41
  • 13
    Also note that the == is also known as the "lets-see-what-I-can-make-of-this" operator, as it results in such pearls as "100" == "1e2" and 0 == "one". – Ants Aasma Jul 13 '09 at 06:52
  • 1
    Not knowing much about PHP, i understand the 100 = 1e2 (10*10^2) but i don't understand the "0" == "one" ? Can someone explains this to me ? – Ksempac Jul 13 '09 at 07:28
  • 4
    I think he meant "1" == "one". The == operator is like saying to php that he is allowed to parse en process the left and right side expressions in such ways the values become equal. The === is like saying, do a binary comparison on this right here. – Dykam Jul 13 '09 at 08:31
  • 2
    @Ksempac: the second string "one" doesn't parse as the number 1, but the number 0, thus they are equal. – Tim Sylvester Jul 13 '09 at 17:14
  • Ah, that is gross. Does it look for the first char, and parse o to 0? – Dykam Jul 14 '09 at 09:01
  • No, not that bad, it's trying to parse a number and finds no digits, so it gives up and returns zero. Any non-numeric string will behave the same way. – Tim Sylvester Jul 14 '09 at 14:01
  • Ah, that way... returning -1 wouldn't fit too. Hence why other languages have a exception system. – Dykam Jul 14 '09 at 16:32
  • @Dykam: I think he really meant `0 == "one"`, not `"1" == "one"`: the former will actually coerce the string "one" the number 0, and the expression will evaluate as `true`, whereas the latter will not apply any coercion, consider the strings as strings, and evaluate to `false` – MestreLion Aug 04 '18 at 10:39
59

http://www.php.net/ternary

$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;
True
> "5" === 5;
False
Community
  • 1
  • 1
Dykam
  • 10,190
  • 4
  • 27
  • 32
  • Not true. The three equal signs return false if $a and $b are objects of the same class, and have same values for all their (respective) properties. Try it yourself: class FirstClass { public $a; public $b; function FirstClass() { $this -> a = 1; $this -> b = 1; } } $first = new FirstClass(); $second = new FirstClass(); if($first === $second) { echo "triple equal!"; } else { echo "triple not equal!"; } – Vladimir Despotovic Mar 15 '16 at 12:25
  • Dykam, exactly my point - but I am not comparing anything. I just say that in case of objects it is not the VALUE that is compared via === , but the actual reference. IE. they can be completely the same objects, that is, with completely same all the values, but still === shows false, because they are not the same reference. – Vladimir Despotovic Mar 15 '16 at 14:24
  • 1
    You said: $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. I simply say this is not true at all. Full stop. – Vladimir Despotovic Mar 15 '16 at 14:27
  • Simple copy and paste from php.net doesn't always mean it is correct. – Vladimir Despotovic Mar 15 '16 at 14:28
  • If they're actually the same object, they'll compare fine. The value of the reference is compared, not the instance it's pointing to. If anything, it's a matter of definition, but this is the most common one I've encountered. – Dykam Mar 15 '16 at 16:09
  • "$a === $b Identical TRUE if $a is equal to $b, and they are of the same type." They're both references, of the same type, ergo, gettype($first) === "object". Not "FirstClass" or whatever. More documentation is here: http://php.net/manual/en/language.oop5.object-comparison.php If anything, `==` is the odd one out, like usually, doing instance-value based comparison. – Dykam Mar 15 '16 at 16:13
12

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.

Community
  • 1
  • 1
Artem Barger
  • 40,769
  • 9
  • 59
  • 81
  • I feel rather stupid now, that you found it that quickly, I tried to google it without much success.. Thanks everyone anyways. –  Jul 13 '09 at 06:43
  • 2
    in php.net you have answers on 99% of your question regarding it. – Artem Barger Jul 13 '09 at 06:46
12

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
farzad
  • 8,775
  • 6
  • 32
  • 41
7

$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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
anik4e
  • 493
  • 8
  • 16
4

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 ==

Keith
  • 150,284
  • 78
  • 298
  • 434
3

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

Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
umar
  • 4,309
  • 9
  • 34
  • 47
1

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.

Extrakun
  • 19,057
  • 21
  • 82
  • 129
0

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