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"
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 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:
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.
This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 8 years ago.
<?php
var_dump($isHoster); // prints int(0)
if ($isHoster == 'all')
$conditionsHoster = '0, 1';
else
$conditionsHoster = intval($isHoster);
var_dump($conditionsHoster); // prints string(4) "0, 1"
?>
What is going on?? Who can explain that?
This never happened to me...
0 == 'all' is true in php because php tries to convert 'all' to int and (int) 'all' is 0; you should write
if ($isHoster === 'all')
var_dump($isHoster); // prints int(0)
if ($isHoster === 'all')
$conditionsHoster = '0, 1';
else
$conditionsHoster = intval($isHoster);
var_dump($conditionsHoster);
Its because you are comparing Int with String.
Before comparing convert $isHoster to string like $isHoster = (string) $isHoster; or user === to compare.