php -a
Interactive shell
php > var_dump("some" == 0);
bool(true)
php > var_dump("some" == false);
bool(false)
php > var_dump("some" == true);
bool(true)
php > var_dump(false == 0);
bool(true)
My question is simple. What is the reasoning behind ("some" == 0) === true? I understand that due to the weak typing the operands are converted to something comparable. But in this particular example it's against how I understand it.
If the zero would be converted to string it would be:
php > var_dump("some" == "0");
bool(false)
If it was converted to bool, it would be:
php > var_dump("some" == false);
bool(false)
Only sensible explanation is:
php > var_dump(intval("some"));
int(0)
php > var_dump(intval("some") == 0);
bool(true)
But that's certainly not what I or anyone would expect. Especially because any string would be turned to zero (and that potentially means false in any comparison).
Why is it this way?
Update:
My question was more about "why" not "if" or "how", sorry if that was not quite obvious. And I think I found my answer in Why PHP casts two numerical strings to numbers before [loosely] comparing them?
Simples
php > var_dump("some" == 0);
The string "some" is cast to an integer for comparison with an integer, and will be cast to a 0 in this case.
0 == 0 is true
If the string "13 monkeys" was cast to an integer, it would be a value of 13
php > var_dump("13monkeys" == 0);
13 == 0 is false
The rules for casting a string to a number are explained in the PHP docs
Related
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.
I have the following PHP expression
($g == $key)
Values: $g = objid and $key = 0
If I dump the expression
var_dump(array($g == $key,$g, $key));
the result is
array(3) { [0]=> bool(true) [1]=> string(5) "objid" [2]=> int(0) }
Why is it showing true? I solve the problem using === in my expression but I would like to know why is this happening.
PHP is converting your "objid" to an int, and is probably making it a 0, so yeah, your "integerized string" is really equal to 0.:
php > var_dump(' ' == 0, 'foobar' == 0, 'objid' == 0);
bool(true)
bool(true)
bool(true)
Note that if your 'objid' string STARTED with a number, then it would kinda/sorta/not-really work:
php > var_dump('a1b' == 0, '1ab' == 0);
bool(true)
bool(false)
And using the strict comparison === operator would also solve this. It compares values AND types. while 'foo' == 0 is true, 'foo' === 0 is false.
From the manual:
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.
"objid " is not a valid number so it's converted to 0.
The === is a type-safe equality operator - it makes sure that the objects you are comparing are of the same type.
== checks if the two objects have the same 'truthy' or 'falsey' value.
Ex:
0 == '0'
is true because '0' is considered a falsey value, but
0 === '0'
is false because one's an int and the other's a string
this is code:
$s = 0;
$d = "dd";
if ($s == $d) {
var_dump($s);
die(var_dump($d));
}
result is:
int 0
string 'dd' (length=2)
Please explain why.
why ($s == $d) results as true?
Of course, if === is used it will results as false but why this situation requires ===?
Shouldn't it be returned false in both situations?
Because (int)$d equals with 0 and 0=0
you must use strict comparison === for different character tyes (string) with (int)
Your $d is automatically converted to (int) to have something to compare.
When you compare a number to a string, the string is first type juggled into a number. In this case, dd ends up being juggled into 0 which means that it equates to true (0==0).
When you change the code to:
<?php
$s = 1;
$d = "dd";
if ($s == $d)
{
var_dump($s);
die(var_dump($d));
}
?>
You will find that it doesn't pass the if statement at all.
You can more details by reading up on comparison operators and type juggling.
The string "dd" is converted to int, and thus 0.
Another example :
if ( "3kids" == 3 )
{
return true;
}
And yes, this returns true because "3kids" is converted to 3.
=== does NOT auto convert the items to the same type.
Also : 0 == false is correct, but 0 === false is not.
See : http://php.net/manual/en/language.types.type-juggling.php
The string will try to parsed into a number, returns 0 if it is not in right number format.
As seen in the php website :
http://php.net/manual/en/language.operators.comparison.php
var_dump(0 == "a"); // 0 == 0 -> true
In PHP, == should be pronounce "Probably Equals".
When comparing with ==, PHP will juggle the file-types to try and find a match.
A string with no numbers in it, is evaluated to 0 when evaluated as an int.
Therefore they're equals.
Consider the following PHP statements:
echo ( 0 == '' ? 1 : 0); // Output is 1
echo ('0' == '' ? 1 : 0); // Output is 0
The first will print 1, and the second will print 0. Shouldn't they both print 0? Is this expected behavior? I'm guessing its because the second is a string and the first is not, but I'd like another answer.
Read Loose comparisons with ==
http://php.net/manual/en/types.comparisons.php
In Php empty string, NULL and 0 are equal.
In second case '0' is another string, and '' is another. So you get result false.
"", 0, "0", False, array(), Null are all considered False in PHP.
You can use === to make them different.
It's correct behavior. 0 is equal to empty string as well as to false/null.
It is because the first line is not comparing two variables of the same type:
echo ( 0 == '' ? 1 : 0); // Output is 1
echo ('0' == '' ? 1 : 0); // Output is 0
Observe the following:
echo ( 0 === '' ? 1 : 0); // Output is 0
The === operator does a type sensitive equality check, which seems to be what you are expecting. == will do a bit of a fuzzy check if the two arguments are not of the same type.
You should check out the comparison operators documentation for more information:
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.
I think the first one is interpreting both the 0 and the empty string as "false" and they are coming out as equal. PHP has lots of quirks around what it will interpret as true/false.
The second is comparing the string '0' to a blank string and coming up with unequal.
To make sure you compare actual values you should use the === comparison
0 == '' == null
but '0' is a string.
You'd better use === to check the difference of 0, '0', null
php > var_dump(0);
int(0)
php > var_dump('0');
string(1) "0"
php > var_dump(null);
NULL
php > var_dump('');
string(0) ""
You can find the comparison logic here:
http://php.net/manual/en/language.operators.comparison.php
Type of Operand 1 Type of Operand 2 Result
null or string string Convert NULL to "", numerical or lexical comparison
string, resource string, resource Translate strings and resources to numbers, usual math
or number or number
That means for '0' == '', the first case applies, and lexical comparison is used. And "0" and "" are not equal according to lexical comparison.
For 0 == '', the second case applies. '' is converted to a number, 0 in this case, which is equal to 0.
'0' == '' will always be false. Because symbol '0' is not an empty symbol. But in PHP 0 (zero) and empty symbol are the same.
It is in this way:
$arr_val = array(0,1,'0','1');
foreach ($arr_val as $key){
echo ($key == "TEST")?"EQUALLED":"NOT EQUALLED"."<br>";
}
0 == "TEST" prints "EQUALLED"
1 == "TEST" prints "NOT EQUALLED"
'0' =="TEST" prints "NOT EQUALLED"
'1' =="TEST" prints "NOT EQUALLED"
When I say it prints the value "SELECTED". But why the above first case prints equalled. Any ideas on this please? How would this be equal to. We know the fix to do comparision with
(===) operator. But I am trying to know the reason why (0=="TEST") is true.
When you provide PHP's == operator with a mixture of numeric and string operands, PHP will attempt to convert the string to the matching numeric type, part of a process it calls "type juggling". In this case, the string "TEST" converts to integer 0 so your test is equivalent to 0 == 0 which is true.
PHP provides the === operator, for testing if the value and type of both operands are equal. So while 0 == "TEST" will evaulate to true, 0 === "TEST" will not, and neither will 0 === "0" or 0 === 0.0.
Note that when PHP converts a string to a number, it attempts to parse the string for a valid number. See intval for more information on how it does this. Had you'd written 0 == "1TEST", the expression would have evaulated to 0 == 1, or false.
In your second example, 1 == "TEST", the string "TEST" is again converted to an integer resulting in 1 == 0, which is false.
Your last two examples use string comparisons. There is no conversion involved, and the results are self-explanatory.
PHP provides a comprehensive breakdown of how variables of different type compare for equality.
Because 0 is an integer, behind the scenes, this is the comparison that happens:
0 == intval( "TEST" )