php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80502 Comparison of empty string to 0
Submitted: 2020-12-10 12:18 UTC Modified: 2020-12-10 12:49 UTC
Votes:6
Avg. Score:4.0 ± 0.8
Reproduced:4 of 5 (80.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: craig at craigfrancis dot co dot uk Assigned:
Status: Verified Package: *General Issues
PHP Version: 8.0.0 OS: N/A
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: craig at craigfrancis dot co dot uk
New email:
PHP Version: OS:

 

 [2020-12-10 12:18 UTC] craig at craigfrancis dot co dot uk
Description:
------------
Maybe related to RFC "string_to_number_comparison"?

In PHP 7 the comparison ('' < 0) would convert the empty string to 0, then return false.

But in PHP 8.0.0, the empty string is now considered less-than 0?

Common issue for HTML forms, which provide all values as strings, and a blank number field is effectively seen as 0 - e.g. entering a time with separate fields (hours and minutes), and the user does not enter a value in the seconds field... yes, you could cast the value to an integer, but a lot of websites out there don't.

Test script:
---------------
var_export([
    (''  < 0),
    ('1' < 0),
    ('0' < 0),
  ]);

Expected result:
----------------
array ( 0 => false, 1 => false, 2 => false, )

Actual result:
--------------
array ( 0 => true, 1 => false, 2 => false, )

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-10 12:27 UTC] craig at craigfrancis dot co dot uk
And the same with ('' == 0)... PHP 7 this would be true, PHP 8 it's false.

While I appreciate that's supposed to be false with ('' === 0), the double equals comparison operator is supposed to be equal "after type juggling".
 [2020-12-10 12:49 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2020-12-10 12:49 UTC] cmb@php.net
Yes, that is indeed a consequence of that RFC.  Since the empty
string is not a well-formed numeric string, a string comparison is
done, and the empty string is less than any other string.  The
migration guide should not only mention == comparision, but the
other affected operations as well.  And of course the respective
documentation in the manual proper needs to be updated.
 [2020-12-10 12:49 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2020-12-10 14:21 UTC] craig at craigfrancis dot co dot uk
https://www.php.net/manual/en/language.operators.comparison.php

Operand 1: string, resource, int or float
Operand 2: string, resource, int or float
Result: Translate strings and resources to numbers, usual math

I have a horrible feeling this change is going to cause issues, as PHP is typically working with user input from web forms, where the GET/POST values provided are strings, same with many other sources (e.g. fgetcsv).
 [2022-11-04 02:25 UTC] php dot net at sameprecision dot org
Indeed this is causing problems and breaks compatibility with javascript where 0 == "".  With PHP 8, 0 != "".

What RFC is being referenced with this?  Why would coercion to string "0" make sense here to have "0" != "" instead of false == false or 0 == 0?
 [2022-11-04 02:46 UTC] php dot net at sameprecision dot org
Why not stick with the most prolific scripting language on earth, javascript? 0 == ""
 [2023-01-25 09:22 UTC] kimia dot saladdmo9881 at gmail dot com
That was so great.

(https://www.fmc4me.net/)github.com
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC