views:

610

answers:

10

I recently studied some code that I'm supposed to use for different reasons, it's unrelevant. The thing I've noticed is someone using the operator "===" which I can't make sense out of. I've tried it with a function and it corresponds in crazy ways. The language is PHP by the way.

Does anyone know what the definition of this operator is, I can't even find it in the declaration of php operators.

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

PHP Docs

Tim Sylvester
As a note, this equality operator also appears in Javascript and I believe Perl. It's fairly common.
Stuart Branham
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
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
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
@Ksempac: the second string "one" doesn't parse as the number 1, but the number 0, thus they are equal.
Tim Sylvester
Ah, that is gross. Does it look for the first char, and parse o to 0?
Dykam
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
Ah, that way... returning -1 wouldn't fit too. Hence why other languages have a exception system.
Dykam
+1  A: 

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

Jon
+3  A: 

You can read here

Artem Barger
I feel rather stupid now, that you found it that quickly, I tried to google it without much success.. Thanks everyone anyways.
in php.net you have answers on 99% of your question regarding it.
Artem Barger
+16  A: 

http://www.php.net/ternary

$a == $b Equal TRUE if $a is equal to $b.

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

> "5" == 5;
True
> "5" === 5;
False
Dykam
+1  A: 

See http://www.wellho.net/mouth/863_Double-and-Triple-equals-operator-in-PHP.html 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.

A: 

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
+1  A: 

in PHP you may compare 2 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

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

if you use == as comparison operator with 2 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 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

farzad
A: 

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

A: 

When you starting seeing anything repeated three times you know the guy typing has turrets.

Xeoncross
A: 

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