Why below code is printing the "here", it should be "there"
$a = "171E-10314";
if($a == '0')
{
echo "here";
}
else
{
echo "there";
}
Why below code is printing the "here", it should be "there"
$a = "171E-10314";
if($a == '0')
{
echo "here";
}
else
{
echo "there";
}
PHP automatically parses anything that's a number or an integer inside a string to an integer. "171E-10314"
is another way of telling PHP to calculate 171 * 10 ^ -10314
, which equates to (almost) 0
. So when you do $a == '0'
, the '0'
will equate to 0
according to PHP, and it returns true
.
If you want to compare strings, use the strcmp function or use the strict comparator ===
.
when you use the == comparison, PHP tries to cast to different data types to check for a match like that
in your case:
check this link for a visual representation: http://www.wolframalpha.com/input/?i=171E-10314
Use === /*(this is for the 30 character)*/
As per the answers given I tried to convert the string into numerical:
$a = "171E-10314" + 0;
print $a;
And I got output as 0.
Thats why it is printing here.