This question already exists:
Closed 13 years ago.
Possible Duplicate:
php == vs === operator
What's the difference between !== and != in PHP?
!== is strict not equal and which does not do type conversion
!= is not equal which does type conversion before checking
=== AND !== checks if the values compared have the same type (eg: int, string, etc.) and have the same values
While...
== AND != only compares the values
"1" != 1 // False
"1" !== 1 // True
It's a type thing. !== takes into account the types of its operands, while != does not (the implicit conversion makes the first conditional false).
== is only true if the values are equal.
=== is only true if the values and types are equal.
the triple equal also make sure the two variable are from the same type
1 == `1` // is ok
1 === `1` // is not same.
Both are comparion operators
$a !== $b Return TRUE if $a is not equal to $b, or they are not of the same type.
$a != $b Return TRUE if $a is not equal to $b.
Related
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.
This question already has answers here:
Comparing String to Integer gives strange results
(5 answers)
Closed 6 years ago.
I have this code:
$test = 0;
if ($test == "on"){
echo "TRUE";
}
Result of this code will be:
TRUE
WHY??? My version of PHP: 5.4.10.
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.
$test = 0;
if ($test === "on"){
echo "TRUE";
}
PHP will convert the string to number to compare. Using ===, will compare the value as well as the type of data.
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
Docs
Because you are comparing $test with string value not binary value, if you want to compare with string value, try with === comparison, for
value + dataType.
In your example, var_dump(0=="on"); always return bool(true).
But when you use var_dump(0==="on"); it will give you bool(false).
Example from PHP Manual:
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
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.
So use "===" for comparision.
Refer the link : http://php.net/manual/en/language.operators.comparison.php
This is because your are doing ==
0 is integer so in this on converted to int which is 0
So your if statement looks like 0==0
For solution You have to use ===
if ($test === "on")
In PHP, loose comparison between integer and non-numeric string (i.e string which cannot be interpreted as numeric, such as "php" or in your case "on") will cause string to be interpreted as integer with value of 0. Note that numeric "43", "1e3" or "123" is a numeric string and will be correctly interpreted as numeric value.
I know this is weird.
What does this !== mean in php and is there any doc's on it?
PHP comparison operators, "Not identical" (5th in the table)
This operator works much like != but also checks the type of the operands. For example:
3 != '3' is false, but 3 !== '3' is true.
== is the comparison operator you're familiar with: if two values are equivalent, they == each other. There's some type coercion that goes on before the comparison.
4 == '4' // true: equivalent value, different type
=== is a more strict comparison that requires that values be of the same type.
4 === 4 // true: same value, same type
'4' === '4' // true: same value, same type
4 === '4' // false: equivalent value, different type
!== is the opposite of the strict comparison operator, so it is true when two values are of a different type or different value or both.
4 !== 3 // true: different value, same type
4 !== '4' // true: equivalent value, different type
'4' !== 3 // true: different value, different type
'4' !== '3' // true: different value, same type
4 !== 4 // false: same value, same type
It means "not equal or not the same type".
This shows the difference between != and !==:
"5"!=5 //returns false
"5"!==5 //returns true
That is the not identical operator
$a !== $b
Returns TRUE if $a is not equal to $b, or they are not of the same type.
For example, it is used to check if a variable is false and not 0, since 0 is the same that false for PHP.
$bar = 0;
if ($bar != false) { echo '$bar != false'; } // won't output the text
if ($bar !== false) { echo '$bar !== false'; } // will output the text
!= is used for value only
but
!== is used for value and type both
suppose:
$a = "5"; // String
$b = 5; // Integer
$a!=$b // false
$a!==$b // true
That's the difference.
I've always done this: if ($foo !== $bar)
But I realized that if ($foo != $bar) is correct too.
Double = still works and has always worked for me, but whenever I search PHP operators I find no information on double =, so I assume I've always have done this wrong, but it works anyway. Should I change all my !== to != just for the sake of it?
== and != do not take into account the data type of the variables you compare. So these would all return true:
'0' == 0
false == 0
NULL == false
=== and !== do take into account the data type. That means comparing a string to a boolean will never be true because they're of different types for example. These will all return false:
'0' === 0
false === 0
NULL === false
You should compare data types for functions that return values that could possibly be of ambiguous truthy/falsy value. A well-known example is strpos():
// This returns 0 because F exists as the first character, but as my above example,
// 0 could mean false, so using == or != would return an incorrect result
var_dump(strpos('Foo', 'F') != false); // bool(false)
var_dump(strpos('Foo', 'F') !== false); // bool(true), it exists so false isn't returned
!== should match the value and data type
!= just match the value ignoring the data type
$num = '1';
$num2 = 1;
$num == $num2; // returns true
$num === $num2; // returns false because $num is a string and $num2 is an integer
$a !== $b TRUE if $a is not equal to $b, or they are not of the same type
Please Refer to http://php.net/manual/en/language.operators.comparison.php
You can find the info here: http://www.php.net/manual/en/language.operators.comparison.php
It's scarce because it wasn't added until PHP4. What you have is fine though, if you know there may be a type difference then it's a much better comparison, since it's testing value and type in the comparison, not just value.
Is there a difference between !== and != in PHP?
The != operator compares value, while the !== operator compares type as well.
That means this:
var_dump(5!="5"); // bool(false)
var_dump(5!=="5"); // bool(true), because "5" and 5 are of different types
!= is the inverse of the == operator, which checks equality across types
!== is the inverse of the === operator, which checks equality only for things of the same type.
!= is for "not equal", while !== is for "not identical". For example:
'1' != 1 # evaluates to false, because '1' equals 1
'1' !== 1 # evaluates to true, because '1' is of a different type than 1
!== checks type as well as value, != only checks value
$num = 5
if ($num == "5") // true, since both contain 5
if ($num === "5") // false, since "5" is not the same type as 5, (string vs int)
=== is called the Identity Operator. And is discussed in length in other question's responses.
Others' responses here are also correct.
Operator != returns true, if its two operands have different values.
Operator !== returns true, if its two operands have different values or they are of different types.
cheers
See the PHP type comparison tables on what values are equal (==) and what identical (===).