rfc:string_to_number_comparison
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:string_to_number_comparison [2019/02/26 12:46] – typo nikic | rfc:string_to_number_comparison [2020/07/02 08:00] – nikic | ||
---|---|---|---|
Line 8: | Line 8: | ||
===== 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 126: | Line 126: | ||
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 148: | Line 146: | ||
</ | </ | ||
- | 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 189: | Line 170: | ||
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. | ||
- | |||
- | One possible way to mitigate the impact is to introduce an ini setting in PHP 7.4, which will perform the comparison using both the old and the new method and emit a deprecation warning if the results differ. This would allow identifying affected code based on production logs. | ||
===== Vote ===== | ===== Vote ===== | ||
- | TBD | + | Yes/No. |
rfc/string_to_number_comparison.txt · Last modified: 2020/07/31 12:55 by nikic