This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 6 years ago.
I just var_dump(false==0) it outputs bool(true) Why false== 0 is true.I know true==1 is true Because if i echo true; It will output 1 so numeric value of true is 1, But numeric value of false is not 0, because when i echo false; It display nothing(empty), So how can false has same value as 0 AS we know == operator compares the values , if they same it will return true, and if their values is not same it will return false , so in the case of false==0 It should be false. Any idea ?
A boolean TRUE value is converted to the string "1". Boolean FALSE is
converted to "" (the empty string). This allows conversion back and
forth between boolean and string values.
So both false == "" and false == 0 are true. Remember, "0" is not the same as 0.
You can have a check here.
PHP Type Comparison
In short, == is the loose comparison operator which invokes type conversion before comparison. Maybe you should use the strict comparison operator === instead.
The same story goes in JavaScript.
false has the the same value as 0, it is just another way of writing it
So, false == 0 will be the same thing as saying
0 == 0
which returns true because 0 = 0
Related
This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 4 years ago.
I am reading the PHP documentation for boolean.
One of the comments says 0=='all' is true.
http://php.net/manual/en/language.types.boolean.php#86809
I want to know how it becomes true.
The documentation says all non-empty strings are true except '0'.
So 'all' is true
and
0 is false.
false == true should be false.
But:
if(0=='all'){
echo 'hello';
}else{
echo 'how are you ';
}
prints 'hello'.
In PHP, operators == and != do not compare the type. Therefore PHP automatically converts 'all' to an integer which is 0.
echo intval('all');
You can use === operator to check type:
if(0 === 'all'){
echo 'hello';
}else{
echo 'how are you ';
}
See the Loose comparisons table.
As you have as left operand an integer, php tries to cast the second one to integer. So as integer representation of a string is zero, then you have a true back.
If you switch operators you obtain the same result.
As Bhumi says, if you need this kind of comparison, use ===.
If you put a string as condition in a IF steatment it is checked to be not empty or '0', but if you compare it with an integer (==, <, >, ...) it is converted to 0 int value.
if('all')
echo 'this happens!';
if('all'>0 || 'all'<0)
echo 'this never happens!';
This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 4 years ago.
I have this code:
echo ( $arr['process_refunds'] == 'storecredit' ) ? 'true' : 'false'
The value in $arr['process_refunds'] is 0 type int(0).
It tried in php -a and it turns out that if we compare any string with
== with int(0) it evaluates to true.
Why is that?
This is because == is a loosely-typed comparison operator. It is converting your string to a number, therefore it evaluates to true. Try using ===.
Your string is getting converted to a zero because it starts with a letter.
I think it's pretty obvious that the loose comparison is not returning expected values. You can see the comparison table here (2nd table)
http://php.net/manual/en/types.comparisons.php
you can see that this is expected behaviour. This is because php converts the string "php" or "somestr"to a match the number type, making it equal to 0, before making the assessment.
Unless there are other types/conditions you're looking to match with a loose comparison, to get around this, you should use === that will assure you have the matching type.
Firstly:
php -r "echo ( $arr['process_refunds'] == 'storecredit' ) ? 'true' : 'false';"
prints:
false
It is obvious because $arr is undefined. But if it has value 0 then
php -r "echo ( 0 == 'storecredit' ) ? 'true' : 'false';"
prints:
true
because both 0 and 'storecredit' are converted to integers.
Value of (int) 'storecredit' is 0 because of it does not contain any number on start of string. For example string '4ss' would be converted to 4.
You need no use === because in that way you compare both values are the same type and the same content; example
echo 0 === 'somstr'; // returns false
echo 0 == 'somstr'; //return true
This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 6 years ago.
I just wondering below code ! I had never seen and heard before .Why string date is equal to 0 ? Is there any documentation for that..
<?php
$p = "date";
$n = 0;
$m = 1;
var_dump($p == $n);//true
var_dump($p == $m);//false
var_dump($n == $m);//false
?>
Yes, you compare string with int so string is converted to int first. int from "date" string is 0
See this
you have used ==
0 is an int, so in this case it is going to convert 'date' into int. Which is not parseable as one, and will become 0. that is why you are getting true. try === opertor
That's how it works:
Reference : Manual [See the table]
Loose comparisons with ==
"PHP" == 0 is true
"PHP" == 1 is false
Strict comparisons with ===
"PHP" === 0 is false
"PHP" === 1 is false
So is your case with "date"
I learned that the empty string "", 0 and "0" all mean false in php. I wonder does php take that into account when it comes to comparing equality.
$str = "";
echo ($str == "0") ? "yes" : "no"; // prints "no" (A)
echo ($str == 0) ? "yes" : "no"; // prints "yes" (B)
Line A suggests that php is comparing $str and "0" as if they are all strings, ignoring that they both can mean false. But line B is comparing their "false" interpretation.
So is it the case that php firstly checks if the two values have the same type, if so it checks equality assuming the same type; if not, it uses the boolean meanings of the values (is there type casting involved?)
I learned that the empty string "", 0 and "0" all mean false in php.
This statement is false. Empty string, 0 and "0" are false when casted to boolean type. Otherwise they are either empty string, integer zero or string with one character, respectively.
== checks values of two variables. If their types are different, some casting happens and unpredictable (for rookies) results come up.
=== checks values of two variables and their types.
Anyway, when you compare "0" == "", PHP interpreter compares two strings which are different. But when you go with 0 == "" it first changes numeric string to integer. Empty string equals 0. So we end up with 0 == 0 which is true.
Note: "8abc" becomes 8 after casting to integer. abc8 becomes 0 when casted
Manual on:
- Types casting (juggling)
- Types comparison
There are two equality comparator in PHP
When the types are the same, they behave in the same way.
When the types are different, it all depends: PHP does not cast both values to booleans. It depends on the types of both operands, and there is a table to know what PHP will do (see second link).
I recommend reading this stackoverflow question
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Also, the PHP manual for comparison
http://au.php.net/manual/en/language.operators.comparison.php
// double equal will cast the values as needed followin quite complex rules
0 == '0' // true, because PHP casted both sides to numbers
// triple equals returns true only when type and value match
0 === '0' // false
$str = "";
//Comparing two strings, no implicit type conversion happens. Since
// "" is not equal "0", result is FALSE
echo ($str == "0") ? "yes" : "no"; // prints "no" (A)
//Comparing a STRING to an INT,implicit conversion happens to convert
// the string "" to an INT. Since "" has no digits, it's evaluated
// to 0. Hence, result of comparison is TRUE
echo ($str == 0) ? "yes" : "no"; // prints "yes" (B)
Use "===" for more accurate comparison.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
Why this
var_dump(0 == "string");
outputs this
bool(true)
Isn't the context of == operator supposed to convert 0 into FALSE and "string" into TRUE according to this set of rules?
var_dump(0 == "string");
is doing a numeric (integer) comparison
0 is an integer, so "string" is converted to an integer to do the comparison, and equates to an integer value of 0, so 0 == 0 is true
Se the comparison with various types table in the PHP documentation for details
The table shown here is more fit for your case.
It shows TRUE for comparing 0 with "php".
Within the comparison you do not convert both operands to a boolean, but one operand will be converted to match the type of the other operand. In your case the string gets converted to an integer, which results in another 0. This gives you 0 == 0, which yields true.
They are not of the same type, use === if you want to check if they are also of the same type.
PHP: ==
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.
"string" is not number format, so it will be convert to 0.
during the comparison, the string is converted to an integer:
var_dump(0);
var_dump((int)"string");
var_dump(0 == "string");
last line will be automatically converted to:
var_dump(0 == (int)"string");
so this return will return:
int(0)
int(0)
bool(true)
bool(true)
You're looking for the comparison table on this site first: http://php.net/manual/en/language.operators.comparison.php. Casting to bool doesn't apply here.
Operand 1 Operand 2
...
string, resource string, resource Translate strings and resources to numbers,
or number or number usual math
"string" cast to a number equals 0.