This can't be TRUE [duplicate] - php

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"; }

Related

PHP why is 0=='all' true? [duplicate]

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!';

php why does 0 == 'somestr' evaluate to true [duplicate]

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

Why string date is equal to 0 [duplicate]

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"

Simple If else is not working [duplicate]

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.

== in php causing false positives when variable is zero [duplicate]

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

Categories