What does "===" mean? [duplicate] - php

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 4 months ago.
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.

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

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

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.

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

$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
}

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

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

For PHP, there many different meanings a zero can take
it can be a Boolean false
it could be a null value
It could really be a zero
So === is added to ensure the type and the value are the same.

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.

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

Related

type casting and equal operator precedence in php

Why do these 2 statements not output the same result?
The only reason I can imagine is operator precedence which appears to the same for == and ===.
$a = (bool) 4 == 4;
$b = (bool) 4 === 4;
var_dump($a); // bool(true)
var_dump($b); // bool(false)
Yes, operator precedence is the same for == and ===. Clearly the difference here is the operator itself.
First we have to acknowledge that type casting has a higher precedence than these two comparison operators. So, in reality, you're doing:
$a = (TRUE == 4);
$b = (TRUE === 4);
When you do a == you're simply trying to see if the values are equal. Only values of the same type can be compared. Since you start with a boolean, the number 4 will therefore also be turned into a boolean. This is called type juggling. We already know that (bool)4 is TRUE. So $a must be TRUE.
However, when you do a === there is no type juggling, instead it will only return TRUE if the two operands have the same value and type. Since a boolean isn't the same type as an integer $b must be FALSE.

What is the difference in the two not equal operator in php?

There are two not equal operator != and <>. Are they the same thing? Or are they slightly different from one another?
They are equal: http://php.net/manual/en/language.operators.comparison.php
$a != $b // Not equal TRUE if $a is not equal to $b after type juggling.
$a <> $b // Not equal TRUE if $a is not equal to $b after type juggling.
$a !== $b // Not identical TRUE if $a is not equal to $b, or they are not of the same type.
There is no difference. You can use both in MSSQL.
The MSSQL doc says:
!= functions the same as the <> (Not Equal To) comparison
operator.
But <> is defined in the ANSI 99 SQL standard and != is not. So not all DB engines may support it and if you want to generate portable code I recommend using <>.

Why can I infere that TRUE == FALSE in php? [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 6 years ago.
A friend of mine recently showed my the following snippet
<?php
$a = 0;
$b = 'x';
if(FALSE == $a && $a == $b && $b == TRUE) {
echo 'WTF!?';
}
?>
which ouputs WTF!?
I understand why FALSE == $a holds, because zero is considered to be FALSE. I also understand why $b == TRUE holds, because the string is not empty. But I didn't understand why $a == $b is true, could somebody explain to me what type coercion rules play together here to get this rather amusing result?
when you compare $a == $b you are comparing an int with a string so PHP tries to parse the string into an int if it fails which happened in this case, it changes its value to 0 and then 0 == 0 returns true. Check this:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
Change this condition $a == $b to $a === $b to compare the type as well. Hope this helps.

Why does 1234 == '1234 test' evaluate to true? [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
php == vs === operator
An easy answer for someone I'm sure. Can someone explain why this expression evaluates to true?
(1234 == '1234 test')
Because you are using the == (similarity) operator and PHP is coercing the string to an int.
To resolve it use the === (equality) operator, which checks not only if the value is the same, but also if the data type is the same, so "123" string and 123 int won't be considered equal.
In PHP (and JavaScript -- which has slightly different behavior), the comparison operator == works differently than it does in strongly-typed languages like C or Java. The === operator has the behavior that you most likely expect. Below is a breakdown of the two comparison operators as they apply to PHP.
==
This operator is officially known as the "equality" operator, though that doesn't really fit the normal definition of the word "equality". It does what is known as a type-juggling comparison. If the types of both operands don't match (in your example, 1234 was an integer and 1234 test was a string), PHP will implicitly cast the operands to each others' types and test the equality of the newly-typed values as shown below:
<?php
var_dump( (int) 'hi' ); // int(0)
var_dump( (string) 0 ); //string("0")
var_dump( 'hi' == 0 ); // bool(true)
var_dump( (int) '1hi' ); // int(1)
var_dump( 1 == '1hi' ); // bool(true)
It has a counterpart (type-juggling) inequality operator, !=.
===
The === operator, known as the "identical" operator, performs a strict check of the value and type of both operands and does not perform any implicit casts. Therefore, "0" does not === 0 and "1234 test"does not === 1234.
<?php
var_dump( '1234 test' === 1234 ); // bool(false)
It has a counterpart (strict) inequality operator, !==.
Quirks
Note that the === operator has behavior on objects that is considered strange by some. Say we have class A and variables $a and $b as defined below:
<?php
class A {
public $property = 'default value';
}
$a = new A();
$b = new A();
You might expect var_dump($a === $b); to output bool(true). It will actually return false. When used upon objects, the operator actually checks if both operands are references to the same object. The == operator, in this instance, works by checking the properties of the objects, so $a == $b.
PHP Manual Links
Comparison operators
Type juggling
When casting a string to an integer, any numeric characters up to the first non-numeric character becomes the number. Thus '1234 test' becomes 1234 because space is not a numeric character.
Thus 1234 == '1234 test'
If you want to force a string comparison, you should cast to string:
''.(1234) == '1234 test' // implicit
(string) 1234 == '1234 test' // explicit
strval(1234) == '1234 test' // procedural
You are loosely comparing two different types of data (an integer and a string). PHP has a very detailed chart of how comparisons work in their system when using the loose comparison binary operator (==):
http://php.net/manual/en/types.comparisons.php
If you want to ensure that the types are also in sync, that is that they are both integers or both strings, use the strong type comparison operator (===).
Note that, when using this operator, this will also return false:
1234 === '1234'
If you are unsure of your types when comparing, you can couple the strong-type comparison with PHP typecasting:
$a = 1234;
$b = '1234';
if ($a === $b) { } // Will not fire, as it is false
if ((int)$a === (int)$b) { } // Will fire, as it is true
The double equals will tell php to parse an int from the string. The string will evaluate to the integer 1234. Use triple equals '===' to get exact comparison.
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically
var_dump(0 == "a"); // 0 == 0 -> true

PHP empty var == 0?

In php 5
$my_var = "";
if ($my_var == 0) {
echo "my_var equals 0";
}
Why it evaluates true? Is there some reference in php.net about it?
PHP is a weakly typed language. Empty strings and boolean false will evaluate to 0 when tested with the equal operator ==. On the other hand, you can force it to check the type by using the identical operator === as such:
$my_var = "";
if ($my_var === 0) {
echo "my_var equals 0";
} else {
echo "my_var does not equal 0";
}
This should give you a ton of information on the subject: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
A string and an integer are not directly comparable with ==. So PHP performs type juggling to see if there is another sensible comparison available.
When a string is compared with an integer, the string first gets converted to an integer. You can find the details of the conversion here. Basically, since "" is not a valid number, the result of the conversion is 0. Thus, the comparison becomes 0 == 0 which is clearly true.
You'll probably want to use the identity comparison === for most (if not all) your comparisons.
This is due to the type coercion that comes from the equality operator you are using.
The PHP manual has the Type Comparison tables to shed a light on this.
Its generally considered a good practice to utilize the identical operator === instead, as to avoid such corner(?) cases.
In your first line, you define $my_var as string.
Inside the comparison you compare that variable with a constant integer.
If you want exact comparison (I don't know why you need to compare a string with an integer without any cast), you should use the ===:
if ($my_var === 0) {
echo "my_var equals 0";
}
That will never echo the message.
The PHP manual defines in Comparison Operators section, the operator == as:
TRUE if $a is equal to $b after type juggling.
So, the important thing here is type juggling.
As a matter of fact, in PHP Manual: types comparisons, the second table tell you exactly that integer 0 equals string "".
here is the reference in the php manual about boolean values
http://www.php.net/manual/en/language.types.boolean.php
and here is the reference for the NULL value
http://www.php.net/manual/en/language.types.null.php
$my_var = '';
if ($my_var == 0) {
echo "my_var equals 0"
}
evaluates to true because "" is the same as NULL which evaluates to false or 0

Categories