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.
Related
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 5 years ago.
$vartest = 0;
if ($vartest == "This can't be TRUE") { echo "But it is TRUE"; }
>> But it is TRUE
By mistake I made a wrong declaration somewhere in my program, and discovered this strange behaviour.
I used $vartest = āā most of the time, but somewhere $vartest = 0 slipped into the program.
Because I spent hours to find the error Iām posting this just for awareness.
Just one question. The variable $vartest is empty, but why does PHP find this TRUE ?
You had to compare on equality and type with "===" and not only with "==" for equality with typeconversion. Try this:
$vartest = 0;
if ($vartest === "This can't be TRUE") { echo "But it is 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.
While developing a PHP application, I ran into this strange quirk.
Apparently, the string '01' == '1', '05' == '5', '03111' == '3111'.
I tried this -
php > $numbers = ["1", "2", "3", "4", "5"];
php > in_array("01", $numbers);
true
php > var_dump("01" == "1");
true
php > var_dump("00003333" == "3333")
true
How do I prevent this (i.e return false for the in_array call) , and why is it happening in the first place?
You should use strict comparison in in_array
in_array("01", $numbers, true);
and === instead of ==
then php compare also types
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:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
php == vs === operator [duplicate]
Why does 1234 == '1234 test' evaluate to true? [duplicate]
(6 answers)
Closed 9 years ago.
I have the strangest php behavior I've never noticed before:
$array =array(0,1,2, 'parent');
foreach ($array as $value)
{
if ($value=='parent')
{
echo $value;
echo '<br>';
continue;
}
}
exit;
Will return
0
parent
I was wondering if anyone could explain to me why it matches the 0 to the string 'parent'?
what Mark said but to expound, 0 is a number so the == operator casts the 'parent' string to a number, which gives 0. The == operator does not care about type. So they match.
The "==" operator in php does not compare the type of the objects, it converts the objects to another type. So in your case you are comparing a numerical object with a string object. So it changes the type of your string 'parent'. Since that string does not contain any numerical value it probably changes it to 0.
See
http://il.php.net/manual/en/language.operators.comparison.php and
http://il.php.net/manual/en/language.types.string.php#language.types.string.conversion
for more explanations
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
I know the basic difference between == and === , but can some experienced coders tell me some practical examples for both cases?
== checks if the values of the two operands are equal or not. === checks the values as well as the type of the two operands.
if("1" == 1)
echo "true";
else
echo "false";
The above would output true.
if("1" === 1)
echo "true";
else
echo "false";
The above would output false.
if("1" === (string)1)
echo "true";
else
echo "false";
The above would output true.
Easiest way to display it is with using strings. Two examples:
echo ("007" === "7" ? "EQUAL!" : "not equal");
echo ("007" == "7" ? "EQUAL!" : "not equal");
In addition to #DavidT.'s example, a more practical example is the following:
$foo = "Goo";
$bar = "Good Morning";
if (strpos($bar,$foo))
echo "Won't be seen, returns false because the result is in fact 0";
if (strpos($bar,$foo) !== false)
echo "True, though 0 is returned it IS NOT false)";