Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-70966

PHP 8.0: Empty string does not cast to 0

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.10.1
    • 3.11
    • General, Questions, Quiz

    Description

      From https://php.watch/versions/8.0#string-number-comparison and release notes:
      https://php.watch/versions/8.0/string-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.
      RFC: https://wiki.php.net/rfc/string_to_number_comparison

      (so they changed what's converted, previously the string was coerced to number, now the opposite)

      For example, the following snippet returns true on PHP7 and false on PHP8:

      var_dump('' == 0);
      

      This causes some errors around Moodle where we expected empty string to be treated as 0.

      See sandbox: http://sandbox.onlinephpfunctions.com/code/0a8af6908807e6da5172584e6c9d0842609bf6e1

      Attachments

        Issue Links

          Activity

            People

              marina Marina Glancy
              marina Marina Glancy
              Tim Hunt Tim Hunt
              Sara Arjona (@sarjona) Sara Arjona (@sarjona)
              CiBoT CiBoT
              Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Safat Shahin, Tim Hunt, Andrew Lyons, Huong Nguyen, Jun Pataleta, Michael Hawkins, Shamim Rezaie, Simey Lameze, Stevani Andolo, Tim Hunt, Andrew Lyons, Huong Nguyen, Jun Pataleta, Michael Hawkins, Shamim Rezaie, Simey Lameze, Stevani Andolo
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                17/May/21

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 1 hour, 20 minutes
                  1h 20m