rfc:saner-numeric-strings
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:saner-numeric-strings [2020/07/13 16:05] – W.I.P. Bitwise op explanation and warning to type error promotion girgias | rfc:saner-numeric-strings [2020/11/25 12:46] (current) – Add implentation version number girgias | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Saner numeric strings ====== | ====== PHP RFC: Saner numeric strings ====== | ||
- | * Version: 1.3 | + | * Version: 1.4 |
* Date: 2020-06-28 | * Date: 2020-06-28 | ||
+ | * Original Author: Andrea Faulds < | ||
+ | * Original RFC: [[http:// | ||
* Author: George Peter Banyard < | * Author: George Peter Banyard < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
* Implementation: | * Implementation: | ||
Line 12: | Line 14: | ||
A string can be categorised in three ways according to its numeric-ness, | A string can be categorised in three ways according to its numeric-ness, | ||
- | * A //numeric string// is a string containing only a [[https:// | + | * A //numeric string// is a string containing only a [[https:// |
- | * A // | + | * A // |
* A // | * A // | ||
A fourth way PHP might deal with numeric strings is when using an //integer// string for an array index. | A fourth way PHP might deal with numeric strings is when using an //integer// string for an array index. | ||
An integer string is stricter than a numeric string as it has the following additional constraints: | An integer string is stricter than a numeric string as it has the following additional constraints: | ||
- | * It doesn' | + | * It doesn' |
* It doesn' | * It doesn' | ||
Line 27: | Line 29: | ||
" | " | ||
" | " | ||
- | " 1" => " | + | " 1" => " |
" | " | ||
]; | ]; | ||
Line 43: | Line 45: | ||
string(22) " | string(22) " | ||
[" 1" | [" 1" | ||
- | string(19) " | + | string(19) " |
[" | [" | ||
string(5) " | string(5) " | ||
Line 101: | Line 103: | ||
var_dump(123 + " | var_dump(123 + " | ||
</ | </ | ||
- | * Increment/Decrement | + | * Increment/decrement |
$a = " | $a = " | ||
var_dump(++$a); | var_dump(++$a); | ||
Line 118: | Line 120: | ||
</ | </ | ||
* Bitwise operations, e.g.< | * Bitwise operations, e.g.< | ||
- | TODO | + | var_dump(123 & " |
+ | var_dump(123 & " | ||
+ | var_dump(123 & " | ||
+ | var_dump(123 & " | ||
+ | var_dump(123 & " | ||
</ | </ | ||
Line 130: | Line 136: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | Unify the various numeric string modes into a single concept: Numeric characters only with both leading and trailing | + | Unify the various numeric string modes into a single concept: Numeric characters only with both leading and trailing |
+ | |||
+ | This means, all strings which currently emit the < | ||
+ | |||
+ | One exception to this are type declarations as they only accept proper numeric strings, thus some < | ||
- | This means, all strings which currently emit the < | ||
For string offsets accessed using numeric strings the following changes will be made: | For string offsets accessed using numeric strings the following changes will be made: | ||
- | * Leading numeric strings will emit the “Illegal string offset” instead of the “A non well formed numeric value encountered” notice, and continue to evaluate to their respective values. | + | * Leading numeric strings will emit the “Illegal string offset” |
* Non-numeric strings which emitted the “Illegal string offset” warning will throw an “Illegal offset type” TypeError. | * Non-numeric strings which emitted the “Illegal string offset” warning will throw an “Illegal offset type” TypeError. | ||
- | * A secondary implementation vote will decide | + | * There is a secondary implementation vote to decide |
Line 146: | Line 155: | ||
foo(" | foo(" | ||
</ | </ | ||
- | * < | + | * < |
- | var_dump(is_numeric(" | + | var_dump(is_numeric(" |
</ | </ | ||
* String offsets< | * String offsets< | ||
$str = 'The world'; | $str = 'The world'; | ||
var_dump($str[' | var_dump($str[' | ||
- | var_dump($str[' | + | var_dump($str[' |
var_dump($str[' | var_dump($str[' | ||
</ | </ | ||
Line 166: | Line 175: | ||
* String-to-string comparisons< | * String-to-string comparisons< | ||
var_dump(" | var_dump(" | ||
+ | </ | ||
+ | * Bitwise operations, e.g.< | ||
+ | var_dump(123 & " | ||
+ | var_dump(123 & " | ||
+ | var_dump(123 & " | ||
</ | </ | ||
Line 175: | Line 189: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | There are two backward incompatible changes: | + | There are three backward incompatible changes: |
- | * Code relying on numerical strings with trailing | + | * Code relying on numerical strings with trailing |
* Code with liberal use of leading-numeric strings might need to use explicit type casts. | * Code with liberal use of leading-numeric strings might need to use explicit type casts. | ||
+ | * Code relying on the fact that < | ||
The first reason is a precise requirement and therefore should be checked explicitly. A small poly-fill to check for the previous < | The first reason is a precise requirement and therefore should be checked explicitly. A small poly-fill to check for the previous < | ||
Line 184: | Line 199: | ||
Breaking the second reason will allow to catch various bugs ahead of time, and the previous behaviour can be obtained by adding explicit casts, e.g.: | Breaking the second reason will allow to catch various bugs ahead of time, and the previous behaviour can be obtained by adding explicit casts, e.g.: | ||
<PHP> | <PHP> | ||
- | var_dump((int) " | + | var_dump((int) " |
- | var_dump((float) " | + | var_dump((float) " |
- | var_dump((int) " | + | var_dump((int) " |
var_dump((float) " | var_dump((float) " | ||
</ | </ | ||
+ | |||
+ | The third reason already emitted an < | ||
===== Proposed PHP Version ===== | ===== Proposed PHP Version ===== | ||
Line 205: | Line 222: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
* Nikita Popov' | * Nikita Popov' | ||
- | * Adding an E_NOTICE for numerical strings with leading/ | + | * Adding an E_NOTICE for numerical strings with leading/ |
- | * Adding a flag to < | + | * Adding a flag to < |
* Align string offset behaviour with array offsets | * Align string offset behaviour with array offsets | ||
- | * Promote remaining | + | * Promote remaining warnings to Type Errors in PHP 9 |
* Warn on illegal offsets when used within < | * Warn on illegal offsets when used within < | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
- | Per the Voting RFC, there would be a single Yes/No vote requiring a 2/3 majority. | + | Per the Voting RFC, there is a single Yes/No vote requiring a 2/3 majority |
+ | |||
+ | Primary vote: | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | Secondary vote: | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | A pull request for a complete PHP interpreter patch, including | + | A pull request for a complete PHP interpreter patch, including test files, can be found here: https:// |
A language specification patch still needs to be done. | A language specification patch still needs to be done. | ||
Line 234: | Line 263: | ||
===== Changelog ===== | ===== Changelog ===== | ||
- | * 2020-07-13: Tweak inconsistency in regards to Arithmetics/Bitwise ops | + | * 2020-07-13: Tweak inconsistency in regards to Arithmetic/Bitwise ops |
* 2020-07-10: Major rewrite | * 2020-07-10: Major rewrite | ||
* 2020-07-02: Explain difference between array and string offsets, and how the RFC will impact string offsets | * 2020-07-02: Explain difference between array and string offsets, and how the RFC will impact string offsets | ||
* 2020-07-01: Add explicit cast behaviour for leading numeric strings | * 2020-07-01: Add explicit cast behaviour for leading numeric strings | ||
* 2020-06-28: Initial version | * 2020-06-28: Initial version |
rfc/saner-numeric-strings.1594656315.txt.gz · Last modified: 2020/07/13 16:05 by girgias