Bug #80502 [Opn->Ver]: Comparison of empty string to 0
| From: | cmb@php.net | Date: | Thu, 10 Dec 2020 12:49:09 +0000 |
| Subject: | Bug #80502 [Opn->Ver]: Comparison of empty string to 0 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230996@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80502&edit=1
ID: 80502
Updated by: cmb@php.net
Reported by: craig at craigfrancis dot co dot uk
Summary: Comparison of empty string to 0
-Status: Open
+Status: Verified
Type: Bug
Package: *General Issues
Operating System: N/A
PHP Version: 8.0.0
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2020-12-10 12:33:42] rtrtrtrtrt at dfdfdfdf dot dfd
while i agree in the case of == which was always luck for < and > it's idiotic
https://www.php.net/manual/de/migration80.incompatible.php
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.
Comparison Before After
0 == "0" TRUE TRUE
0 == "0.0" TRUE TRUE
0 == "foo" TRUE FALSE
0 == "" TRUE FALSE
42 == " 42" TRUE TRUE
42 == "42foo" TRUE FALSE
------------------------------------------------------------------------
[2020-12-10 12:27:28] 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:18:56] 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, )
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80502&edit=1