I don't understand what is happening in this code... why does my PHP tell me that the string is equal to 0
? Is it a bug in my PHP?
Code:
$loremIpsum="Lorem ipsum";
$cero=0;
if($loremIpsum==$cero){
echo $loremIpsum." es igual a ".$cero.PHP_EOL;
}
Departure:
Lorem ipsum es igual a 0
Code:
$loremIpsum="Lorem ipsum";
$cero="Lorem ipsum";
if($loremIpsum==$cero){
echo $loremIpsum." es igual a ".$cero.PHP_EOL;
}
Departure:
Lorem ipsum es igual a Lorem ipsum
It is not a bug, it is the way in which the conditional can be interpreted. If you compare with double equals "==" you can compare with boolean, int, float, numeric values. To correctly compare a string you can use "===" (for a strict comparison).
When comparing any string regardless of its length, it is most convenient to use a strict condition. Since internally the string, when comparing it with a numerical value, it is parsed. In a nutshell your code behaves like this.
This is because of how the Comparison Operators (
==
) works.How does the conversion from string to numbers work ?
That is, if the comparison is equivalent to the following:
Building on the answers above, there are other implicit conversion types that can lead to curious cases. For example, a boolean compared to a number by
==
converts the number 0 tofalse
and other numberstrue
to so that if you do the following flow of comparisons:You have as answer:
Which would break the transitivity.
I've read the other answers but noticed that something important hasn't been detailed regarding type conversion when using PHP 's "flexible" (
$a==$b
for example) comparison operator .When using the flexible comparison operator between a text string (type string) and a number (type int for example) PHP automatically converts the text string to a numeric value exactly equal to "0" in order to perform the comparison with the another number since this operator is responsible for evaluating the value of both variables when they have the same type (both numerical).
It is necessary to make it clear that PHP does not convert to any number the variable that has a text string and that you want to compare with any number. Actually the process checks if it was already a number, if so, it takes as value the number exactly as it was expressed in the text string. Otherwise, since this value cannot be converted to a numeric value (because the text string does not have any numeric character), it is immediately converted to "0" in order to compare the values, since the type of both variables is the same.
That is why this conditional ( if ) is true:
While in this other no:
It does not enter the conditional because
$loremIpsum
in the type conversion it will have the value "0" and 0 is obviously different from 1 (value of $uno).As other users mention, to solve this, there is precisely the PHP strict comparer (===) that will compare both the type and the content of both variables (without any implicit type conversion):
To expand a little more information you can visit Flexible Comparison and Strict Comparison .
Observation:
1) If we have:
The variable
$a
will be converted to numeric and will be given the value of 0 because the text string has no numeric equivalent.2) If we have:
The variable
$a
will be converted to its numeric equivalence and because the text string has numeric characters, it is assigned just the value of the numbers found in it when the conversion of types to numeric is done, so the new value of$a
will be from 2500I hope it is useful. A greeting!
First point, the comparison must be made with a triple equal sign, as I show you below:
What happens or why does this situation occur?
we have three operators:
= which is used to assign a value to a variable, example:
== that helps me to compare if a value is equal to another because of its type
=== which helps me identify if a value is equal and of the same type
I leave you the recommendation to implement the last one since it makes the comparison stricter
Cheers