rfc:string_to_number_comparison

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:string_to_number_comparison [2020/07/02 08:00] nikicrfc:string_to_number_comparison [2020/07/31 12:55] (current) nikic
Line 2: Line 2:
   * Date: 2019-02-26   * Date: 2019-02-26
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Status: Under Discussion+  * Status: Implemented
   * Target Version: PHP 8.0   * Target Version: PHP 8.0
   * Implementation: https://github.com/php/php-src/pull/3886   * Implementation: https://github.com/php/php-src/pull/3886
Line 118: Line 118:
                          // Before | After | Type                          // Before | After | Type
 var_dump(42 == "   42"); // true   | true  | well-formed var_dump(42 == "   42"); // true   | true  | well-formed
-var_dump(42 == "42   "); // true   | false | non well-formed+var_dump(42 == "42   "); // true   | false | non well-formed (*)
 var_dump(42 == "42abc"); // true   | false | non well-formed var_dump(42 == "42abc"); // true   | false | non well-formed
 var_dump(42 == "abc42"); // false  | false | non-numeric var_dump(42 == "abc42"); // false  | false | non-numeric
 var_dump( 0 == "abc42"); // true   | false | non-numeric var_dump( 0 == "abc42"); // true   | false | non-numeric
 +// (*) Becomes well-formed if saner numeric strings RFC passes
 </code> </code>
  
-A notable asymmetry under the new semantics is that ''%%"   42"%%'' and ''%%"42   "%%'' compare differently. In my opinion both of these should behave the same and ''%%42 == "42   "%%'' should return true. There is a draft RFC [[rfc:trailing_whitespace_numerics|to allow trailing whitespace]] in numeric strings, which would resolve this issue.+A notable asymmetry under the new semantics is that ''%%"   42"%%'' and ''%%"42   "%%'' compare differently. This inconsistency is being addressed by the [[rfc:saner-numeric-strings|saner numeric strings RFC]].
  
 ==== Precision ==== ==== Precision ====
Line 170: Line 171:
  
 This change to the semantics of non-strict comparisons is backwards incompatible. Worse, it constitutes a silent change in core language semantics. Code that worked one way in PHP 7.4 will work differently in PHP 8.0. Use of static analysis to detect cases that may be affected is likely to yield many false positives. This change to the semantics of non-strict comparisons is backwards incompatible. Worse, it constitutes a silent change in core language semantics. Code that worked one way in PHP 7.4 will work differently in PHP 8.0. Use of static analysis to detect cases that may be affected is likely to yield many false positives.
 +
 +Testing with [[https://github.com/php/php-src/pull/3917|a warning on comparison result change]] suggests that the practical impact of this change is much lower than one might intuitively expect, but this likely heavily depends on the type of tested codebase.
  
 ===== Vote ===== ===== Vote =====
  
-Yes/No.+Voting starts 2020-07-17 and ends 2020-07-31. A 2/3 majority is required. 
 + 
 +<doodle title="Change string to number comparison semantics as proposed?" auth="nikic" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
rfc/string_to_number_comparison.1593676807.txt.gz · Last modified: 2020/07/02 08:00 by nikic