PHP String Comparasion to 0 Integer Returns True

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.

Why does someString == 0 evaluate to true in PHP

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)

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.

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.

Comparing different strings in PHP with == returns true

<?php
$a=6064365413078728979;
$b=6064365413078728452;
echo $a."<br>".$b;
//var_dump( $a==$b );
die();
?>

When you run that, then on your machine that might be exceeding limit for a number and that is a numeric comparison taking place. Try the script above and see value for $a will probably be different than the value you gave.

That is why when both are compared numerically they are equal. Hence use === as suggested by others

Edit: Explanation based upon @Axel's Advice.

PHP Manual explains

The size of a float is platform-dependent, although a maximum of
~1.8e308 with a precision of roughly 14 decimal digits is a common
value (the 64 bit IEEE format).

And this website is offering and explanation on the Overflow phenomenon and a small php code to test your system's integer and float range. Getting to know the limit on your servers will most probably explain it best why the offerflow occured

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

checking if 0 is equal to any string always returns true

The PHP Manual has a type Comparison Table in it, which gives you an idea of what happens when comparing variables of two different data types.

Your first example (a 'loose' comparison since it does not also check the data types of the two operands) implicitly converts the string on the left to an integer. Since it does not start with a number, the string is converted to the integer 0, which is equal to the integer 0.

Your second example compares not only the values but the types as well. Since the type is different, the comparison is false.

From this post

Comparing String to Integer gives strange results

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.



Related Topics



Leave a reply



Submit