Comparing String to Integer gives strange results Comparing String to Integer gives strange results php php

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.


Type conversion using the == operator

The == operator is a loosely-typed comparison. It will convert both to a common type and compare them. The way strings are converted to integers is explained here.

Note that the page you linked to doesn't contradict this. Check the second table, where it says that comparing the integer 0 to a string "php" using == shall be true.

What happens is that the string is converted to integer, and non-numeric strings (strings that do not contain or begin with a number) convert to 0.

Numeric vs non-numeric strings

A string that consists of a number, or begins with a number, is considered a numeric string. If the string has other characters after that number, these are ignored.

If a string starts with a character that cannot be interpreted as part of a number, then it is a non-numeric string and will convert to 0. This doesn't mean that a numeric string has to start with a digit (0-9) - for example "-1" is a numeric string because the minus sign is part of a number in that case.

So for example, your string "d85d1d81b25614a3504a3d5601a9cb2e" does not start with a number, so it would convert to 0. But your second string "3581169b064f71be1630b321d3ca318f" would be converted to integer 3581169. So that's why your second test does not work the same way.

What you should do

You probably want:

if ($test1 === "0")

Notice the use of triple equals instead of a double equals. This ensures that what you are comparing is a string that contains the digit zero only, and prevents any type conversion.


After some investigation, it turns out aidan from the PHP manual mentioned that any strings that do not start with a number will be converted to 0 when casted as an integer.

This means that:

("php" == 0) === true("1php" == 0) === false

Very annoying and not well documented. It was at the bottom of the comments on the type comparison page.