rfc:saner-numeric-strings
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:saner-numeric-strings [2020/07/14 15:19] – Improve wording theodorejb | rfc:saner-numeric-strings [2020/08/01 23:37] – RFC was implemented carusogabriel | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 1.4 | * 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 119: | Line 121: | ||
* Bitwise operations, e.g.< | * Bitwise operations, e.g.< | ||
var_dump(123 & " | var_dump(123 & " | ||
+ | var_dump(123 & " | ||
var_dump(123 & " | var_dump(123 & " | ||
var_dump(123 & " | var_dump(123 & " | ||
Line 133: | 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 < | + | This means, all strings which currently emit the < |
One exception to this are type declarations as they only accept proper numeric strings, thus some < | One exception to this are type declarations as they only accept proper numeric strings, thus some < | ||
Line 143: | Line 146: | ||
* Leading numeric strings will emit the “Illegal string offset” warning 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” warning instead of the “A non well formed numeric value encountered” notice, and continue to evaluate to their respective values. | ||
* 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. | ||
- | * There is a secondary implementation vote to decide the following: should numeric strings which correspond to well-formed floats remain a warning (by emitting the same “String offset cast occurred” warning that occurs when a float is used for a string offset), or should the current “Illegal string offset” warning simply be promoted to a < | + | * There is a secondary implementation vote to decide the following: should numeric strings which correspond to well-formed floats remain a warning (by emitting the same “String offset cast occurred” warning that occurs when a float is used for a string offset), or should the current “Illegal string offset” warning simply be promoted to a < |
Line 152: | Line 155: | ||
foo(" | foo(" | ||
</ | </ | ||
- | * < | + | * < |
- | var_dump(is_numeric(" | + | var_dump(is_numeric(" |
</ | </ | ||
* String offsets< | * String offsets< | ||
Line 166: | Line 169: | ||
var_dump(123 + " | var_dump(123 + " | ||
</ | </ | ||
- | * The < | + | * The < |
$d = "5 "; | $d = "5 "; | ||
var_dump(++$d); | var_dump(++$d); | ||
Line 187: | Line 190: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
There are three 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 < | + | * 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 196: | 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) " | ||
</ | </ | ||
Line 219: | 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 warnings to Type Errors in PHP 9 | * 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 for the main proposal. A secondary Yes/No vote requiring a 50%+1 majority will decide whether float strings used as string offsets should continue to produce a warning (with different wording) instead of consistently becoming a TypeError. | + | Per the Voting RFC, there is a single Yes/No vote requiring a 2/3 majority for the main proposal. A secondary Yes/No vote requiring a 50%+1 majority will decide whether float strings used as string offsets should continue to produce a warning (with different wording) instead of consistently becoming a TypeError. |
+ | |||
+ | Primary vote: | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | Secondary vote: | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== |
rfc/saner-numeric-strings.txt · Last modified: 2020/11/25 12:46 by girgias