Home » Php » if statement – Why does (0 == 'Hello') return true in PHP?

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

Hey, if you have got the following code and want to check if $key matches Hello I’ve found out, that the comparison always returns true if the variable is 0. I’ve came across this when an array for a special key and wondered why it’s wasn’t working as expected.
See this code for an example.

$key = 1;
if ($key != 'Hello') echo 'Hello'; //echoes hello

$key = 2;
if ($key != 'Hello') echo 'Hello'; //echoes hello

$key = 0;
if ($key != 'Hello') echo '0Hello'; //doesnt echo hello. why?
if ($key !== 'Hello') echo 'Hello'; //echoes hello

Can anyone explain this?

How to&Answers:

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.

Answer:

Others have already answered the question well. I only want to give some other examples, you should be aware of, all are caused by PHP’s type juggling. All the following comparisons will return true:

  • ‘abc’ == 0
  • 0 == null
  • ” == null
  • 1 == ‘1y?z’

Because i found this behaviour dangerous, i wrote my own equal method and use it in my projects:

/**
 * Checks if two values are equal. In contrast to the == operator,
 * the values are considered different, if:
 * - one value is null and the other not, or
 * - one value is an empty string and the other not
 * This helps avoid strange behavier with PHP's type juggling,
 * all these expressions would return true:
 * 'abc' == 0; 0 == null; '' == null; 1 == '1y?z';
 * @param mixed $value1
 * @param mixed $value2
 * @return boolean True if values are equal, otherwise false.
 */
function sto_equals($value1, $value2)
{
  // identical in value and type
  if ($value1 === $value2)
    $result = true;
  // one is null, the other not
  else if (is_null($value1) || is_null($value2))
    $result = false;
  // one is an empty string, the other not
  else if (($value1 === '') || ($value2 === ''))
    $result = false;
  // identical in value and different in type
  else
  {
    $result = ($value1 == $value2);
    // test for wrong implicit string conversion, when comparing a
    // string with a numeric type. only accept valid numeric strings.
    if ($result)
    {
      $isNumericType1 = is_int($value1) || is_float($value1);
      $isNumericType2 = is_int($value2) || is_float($value2);
      $isStringType1 = is_string($value1);
      $isStringType2 = is_string($value2);
      if ($isNumericType1 && $isStringType2)
        $result = is_numeric($value2);
      else if ($isNumericType2 && $isStringType1)
        $result = is_numeric($value1);
    }
  }
  return $result;
}

Hope this helps somebody making his application more solid, the original article can be found here:
Equal or not equal

Answer:

pretty much any non-zero value gets converted to true in php behind the scenes.

so 1, 2,3,4, ‘Hello’, ‘world’, etc would all be equal to true, whereas 0 is equal to false

the only reason !== works is cause it is comparing data types are the same too

Answer:

Because PHP does an automatic cast to compare values of different types. You can see a table of type-conversion criteria in PHP documentation.

In your case, the string "Hello" is automatically converted to a number, which is 0 according to PHP. Hence the true value.

If you want to compare values of different types you should use the type-safe operators:

$value1 === $value2;

or

$value1 !== $value2;

In general, PHP evaluates to zero every string that cannot be recognized as a number.

Answer:

In php, the string “0” is converted to the boolean FALSE http://php.net/manual/en/language.types.boolean.php