rfc:string_to_number_comparison
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:string_to_number_comparison [2019/02/26 09:03] – created nikic | rfc:string_to_number_comparison [2020/07/17 09:34] – nikic | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2019-02-26 | * Date: 2019-02-26 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.0 | * Target Version: PHP 8.0 | ||
+ | * Implementation: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Comparisons between strings and numbers using '' | + | Comparisons between strings and numbers using '' |
PHP supports two different types of comparison operators: The strict comparisons '' | PHP supports two different types of comparison operators: The strict comparisons '' | ||
Line 117: | Line 118: | ||
// Before | After | Type | // Before | After | Type | ||
var_dump(42 == " | var_dump(42 == " | ||
- | var_dump(42 == " | + | var_dump(42 == " |
var_dump(42 == " | var_dump(42 == " | ||
var_dump(42 == " | var_dump(42 == " | ||
var_dump( 0 == " | var_dump( 0 == " | ||
+ | // (*) Becomes well-formed if saner numeric strings RFC passes | ||
</ | </ | ||
- | A notable asymmetry under the new semantics is that '' | + | A notable asymmetry under the new semantics is that '' |
- | ==== Precision | + | ==== Precision ==== |
- | The reason why the comparison semantics are not simply defined in terms of casting the number to string and performing a non-strict string comparison (even though that is a good way to think about it for most purposes), is that floating-point to string conversions in PHP are subject to two runtime settings: The '' | + | The reason why the comparison semantics are not simply defined in terms of casting the number to string and performing a non-strict string comparison (even though that is a good way to think about it for most purposes), is that floating-point to string conversions in PHP are subject to the '' |
- | Comparisons with well-formed numeric strings are handled separately to be independent of these runtime | + | Comparisons with well-formed numeric strings are handled separately to be independent of this runtime |
- | + | ||
- | An example | + | |
<code php> | <code php> | ||
Line 147: | Line 147: | ||
</ | </ | ||
- | An example of the effect of '' | + | An alternative approach to this issue would be to define that the float to string conversion used for comparisons always uses automatically determined precision ('' |
- | + | ||
- | <code php> | + | |
- | $float = 1.75; | + | |
- | + | ||
- | var_dump($float < " | + | |
- | // Behaves like | + | |
- | var_dump(" | + | |
- | + | ||
- | setlocale(LC_NUMERIC, | + | |
- | var_dump($float < " | + | |
- | // Behaves like | + | |
- | var_dump(" | + | |
- | </ | + | |
- | + | ||
- | It should be mentioned that the setlocale() dependence [[https:// | + | |
- | + | ||
- | An alternative approach to this issue would be to define that the float to string conversion used for comparisons | + | |
==== Special values ==== | ==== Special values ==== | ||
Line 187: | Line 170: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | 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 affect | + | 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 |
- | One possible way to mitigate | + | Testing with [[https:// |
===== Vote ===== | ===== Vote ===== | ||
- | TBD | + | Voting starts 2020-07-17 and ends 2020-07-31. A 2/3 majority is required. |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
rfc/string_to_number_comparison.txt · Last modified: 2020/07/31 12:55 by nikic