skip to Main Content

Lets have a simple PHP script

<?php
var_dump("php" == 0);

According to official documentation (https://www.php.net/manual/en/types.comparisons.php)
, this shoud evaulate into true

BUT
It does, for PHP <=7.4, but no for PHP 8.0

In current version of PHP 8 (8.0.0 – 8.0.2) it evaulates to false.

Same script ran in different PHP docker images

Are there any un-documented changes to this functionality or is this a bug? Or am I missing something?

Thank you

3

Answers


  1. When PHP 8 got released, they also put a release announcement on the website. This is part of one of the new major changes, the saner string to number comparison.

    To quote:

    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.

    Login or Signup to reply.
  2. This behavior is documented in Backward incompatible changes.

    String to Number Comparison

    Non-strict comparisons between numbers and non-numeric strings now
    work by casting the number to string and comparing the strings.
    Comparisons between numbers and numeric strings continue to work as
    before. Notably, this means that 0 == "not-a-number" is considered
    false now.

    Login or Signup to reply.
  3. PHP Documentation maintainer here, PHP 8 did change the semantics and this is shown in the migration guide. However other parts of the documentations are still lagging behind as we don’t have the manpower/time for editing and documenting all the changes related to PHP 8.

    So this is not a bug and more a fact that the current type juggling page is out of date in regards to PHP 8.0.

    It is possible to contribute to the docs via a Pull Request to the GitHub repository.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search