Why Does PHP Consider 0 to Be Equal to a String

Why does PHP consider 0 to be equal to a string?

You are doing == which sorts out the types for you.

0 is an int, so in this case it is going to cast 'e' to an int. Which is not parsable as one and will become 0. A string '0e' would become 0 and would match!

Use ===

From PHP.net:

Comparisons between strings and numbers using == and other non-strict
comparison operators currently work by casting the string to a number,
and subsequently performing a comparison on integers or floats. This
results in many surprising comparison results, the most notable of
which is that 0 == "foobar" returns true.

However this behavior was changed in PHP 8.0:

When comparing to a numeric string, PHP 8 uses a number comparison.
Otherwise, it converts the number to a string and uses a string
comparison.

PHP 7

0 == 'foobar' // true
0 == '' // true
4 == '4e' // true (4e is cast as a number and becomes 4)

PHP 8 converts numbers to strings before making comparisons

0 == 'foobar' // false
0 == '' // false
4 == '4e' // false ('4e' is considered non-numeric therefore 4 is cast as a string and becomes '4')

This is a major change therefore it was implemented in a new major PHP version. This change breaks backward compatibility in scripts that depend on the old behavior.

php why does 0 == 'somestr' evaluate to true

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.

Sample Image

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.

Why is integer 0 equal to a string in PHP?

var_dump(0 == "string");

is doing a numeric (integer) comparison

0 is an integer, so "string" is converted to an integer to do the comparison, and equates to an integer value of 0, so 0 == 0 is true

Se the comparison with various types table in the PHP documentation for details

Why string equal to integer 0 in PHP?

"a" == 0 evaluates to true.

Because any string is converted into an integer when compared with an integer. If PHP can't properly convert the string then it is evaluated as 0. So 0 is equal to 0, which equates as true.

If you want the answer as 0,

you should use === instead of ==,

Because the ordinary operator does not compare the types. Instead it will attempt to typecast the items.

Meanwhile the === takes in consideration type of items.

=== means "equals",

== means "eeeeh .. kinda looks like"

Also, the PHP manual for comparison http://au.php.net/manual/en/language.operators.comparison.php

// double equal will cast the values as needed followin quite complex rules
0 == '0' // true, because PHP casted both sides to numbers

// triple equals returns true only when type and value match
0 === '0' // false

FYI, From the PHP manual:

String conversion to numbers

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

The string will be evaluated as a float if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will be evaluated as an integer.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

Why zero (0) and string are the same in PHP?

No, this is not a bug the string just get's converted to a int. It converts it from left to right until a non numeric value. So since there is a non numeric value right at the start it gets converted to 0.

For more information about String to int see the manual: http://php.net/manual/de/language.types.string.php#language.types.string.conversion

And a quote from there:

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).

So as an example to show that:

echo "5xyz" + 5;  // 5 + 5 -> 10
//^
echo "xyz5" + 5; // 0 + 5 -> 5
//^
echo "x5z" + 5; // 0 + 5 -> 5
//^

Why does (0 == 'Hello') return true in PHP?

The operators == and != do not compare the type. Therefore PHP automatically converts 'Hello' to an integer which is 0 (intval('Hello')). When not sure about the type, use the type-comparing operators === and !==. Or better be sure which type you handle at any point in your program.

0' as a string with empty() in PHP

You cannot make empty() take it. That is how it was designed. Instead you can write an and statement to test:

if (empty($var) && $var !== '0') {
echo $var . ' is empty';
}

You could use isset, unless of course, you want it to turn away the other empties that empty checks for.



Related Topics



Leave a reply



Submit