Why does "someString" == 0 evaluate to true in PHP [duplicate] - php

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 9 years ago.
I'm a little bit confused about truth in PHP.
My understanding is that 0 evaluates to false, and non-empty strings (unless the string is "0") evaluate to true.
This is as I expect:
var_dump((bool) 0); // prints "boolean false"
var_dump((bool) 'someString'); // prints "boolean true"
But then I am surprised by the following result:
var_dump((0=='someString')); // prints "boolean true"
My question is, why does 0=='someString' evaluate to true?

When using the comparison (==) operator strings will be converted to an integer when compared to another integer. This is because of type juggling in PHP. So "someString" evaluates to zero because it is converted to an integer and has no leading digits. If you use the the identical operator (===) type conversions are not done so "someString" is treated a literal string and your statement will then evaluate to false.
The following will evaluate to false when type juggling is performed. Everything else will be evaluated as true:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

Related

Type conversion string / integer [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 1 year ago.
I know that this is caused by type conversion, but can someone explain me, what exactly is happening here, when:
(2 == '2abd') // true
(3 == '3a') // true
I was a bit surprised when running into a bug by comparing a string variable against strings and integers.
Thank you very much!
If we convert 2abd and 3a to integers, you'll get 2 and 3 accordingly.
Since 2 == 2 and 3 == 3 both statements return true.
Online test case:
<?php
var_dump(2 == '2abd'); // true
var_dump(3 == '3a'); // true
var_dump((int) '2abd'); // 2
var_dump((int) '3a'); // 3
var_dump(2 == 2); // true
var_dump(3 == 3); // true
For more info about operator precedence, please take a look at:
PHP's help page regarding this
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Rules about converting to integers

Why does the expression 011 == 11 evaluate to false? [duplicate]

This question already has answers here:
Interview question: In php, is 123==0123?
(5 answers)
Closed 6 years ago.
When I was practicing in php, I noticed, that the following expressions yield strange results:
011 == 11 // false
'011' == 11 // true
Shouldn't they both evaluate to same result?
This is because 011 is treated as an octal value because of the leading 0.
Here's the more in-depth explanation:
The 011 literal is recognized as an octal value
It is then converted to decimal value, which equals to 9
The actual comparison happens which looks like the following: 9 == 11 // false
As of the '011' == 11, it evaluates to true, because when string is compared to integer, it's coerced to the integer value as well. Interestingly, the leading zero in the string is ignored in the proccess and the php interpreter treats the value as a decimal rather than an octal one!

PHP 0 vs False ambiguity [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've been trying to make a system where certain flags in a MySQL database are checked with php. To see if a certain letter is in the string, I use the function strpos(). If the letter I'm checking for happens to be the first one in the string the function returns 0. However this result seems to be synonymous with the Boolean value for false. I tested this theory with the below code:
if(0 == false){
echo 'true';
}
low and behold this evaluates to true and echoes the output inside the block. So, is there a logical way to distinguish between a character being at index 0 or not being part of the string at all? One option that occurred to me was to basically prepend the flags string with some character like an underscore. This way no meaningful characters would ever be at index 0 of the function's return value. That seems like a very dirty patchy way to fix things though. If you have any other ideas I would greatly appreciate your help.
Thanks for reading.
Use ===:
if(0 === false){
// will never be true
echo 'true';
}

what is the difference between == and ===? [duplicate]

This question already has answers here:
What does === do in PHP
(8 answers)
Closed 8 years ago.
I'm learning about cakephp, and I see much conditionals like:
if( x === y){
}
I've looked for it, but I don't find anything.
== compares the values of two variables. If they are of different types, they are converted to a common type and then compared.
===, on the other hand, is more strict. It requires the two sides to be of the same type as well.
php> = 5 == "5"
true
php> = 5 === "5"
false
== wil do auto type conversion, === won't
This means that:
0 == "0" evaluates to TRUE, because internally when comparing strings and numbers, a string is converted to a number when using ==.
0 === "0" evaluates to FALSE, there is no type conversion done and a integer 0 is not equal to a string.
More info in the documentation and more documentation.

IF Statement always executed in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does PHP consider 0 to be equal to a string?
php string comparasion to 0 integer returns true?
it seems that as one has in PHP an if-statement where a function some_function() returns zero
<?php
if( some_function() == "whatever_you_want" ) { ... }
the statement will always be executed since
<?php
echo some_function() == "whatever_you_want";
is then TRUE.
Why behaves PHP in such a counter intuitive way?
This is a defined behavior of PHP when you compare a number value and a string value:
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.
Use strict value comparison with === or !== and you’re getting the expected result.

Categories