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
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)
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
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"
This question already has answers here:
Type-juggling and (strict) greater/lesser-than comparisons in PHP
(3 answers)
Closed 8 years ago.
as I know false is a 0
if(-1 > false)
print "Here";
in this code if returns a true and here is printed
WHY ?
Please see PHP Comparision Operators - table Comparison with Various Types
Type of Operand 1 Type of Operand 2 Result
bool or null anything Convert both sides to bool, FALSE < TRUE
So if you compare bool to anything else, then the second operand is casted to boolean true.
Also we have information here, that FALSE < TRUE, what exactly happens in your example.
In this case it's -1 that is converted into boolean (true, as only 0 is treated as false). So the final comparison is
if (true > false) {
...
}
Type Juggling can be very unintuitive, so always try to avoid situations where you compare variables of two different types. In case of equality comparison always try to use identity operator (===), in case of inequality all you can do is to add a manual cast.
See also: http://us3.php.net/manual/en/types.comparisons.php
< is a numerical comparison operator, the code does loose comparison converting -1 to true and hence the result.
WARNING: -1 is considered TRUE, like any other non-zero
(whether negative or positive) number!
Check with this two code. You can get the difference
if(-1 > false)
print "Here"; //This will print the `Here`
if(-1 > 0)
print "Here"; // Not print `Here`
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.