# [RFC] Permit trailing whitespace in numeric strings

- Thread: https://externals.io/message/104590
- Original archive: https://news-web.php.net/php.internals/104590
- Started by Andrea Faulds on 2019-03-06T00:16:34+00:00
- Messages: 16
- RFC: Trailing whitespace in numeric strings (Withdrawn) — https://externals.io/rfc/trailing_whitespace_numerics.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #104590 — Andrea Faulds — 2019-03-06T00:16:34+00:00

URL: https://externals.io/message/104590#104590

Hej allihopa,

Here's an RFC that's been lying in my drafts for uh… 26 months:

https://wiki.php.net/rfc/trailing_whitespace_numerics

I expect this should be an uncontroversial proposal, but maybe I'm 
jinxing it there. I hope you all like it. :)

Thanks to Nikita for reminding me it existed and thus motivating me to 
pick it up again. Also thanks to Nikita for suggesting a potential 
follow-up RFC, and also making the “Saner string to number comparisons” 
RFC, both providing additional impetus for me to finally clean this up 
and put it to discussion.

Thanks!
Andrea

---

## #104865 — Andrea Faulds — 2019-03-21T23:33:23+00:00

URL: https://externals.io/message/104590#104865

Hi again,

I would proceed to a vote with this (and do intend to still), but the 
response from internals has been very quiet. Has it gotten lost among 
all the other things happening? :)

I realise it's not a very exciting change of course, and there isn't 
much to discuss about it. It's pretty much a like it or hate it RFC with 
no particular nuance beyond that.

One thing I do wonder about (given reaction on reddit and my personal 
general doubt) is how internals feels about whether we should take the 
opposite approach, that is, to break backwards-compatibility by 
rejecting *leading* whitespace in numeric strings instead. All the 
arguments in favour of this RFC's approach can also apply to that 
approach if you flip them around. The main reason to go for what I did 
so far is, well, it's harder to justify backwards-compatibility 
breakages. But I also initially came up with this when PHP 8 wasn't on 
the horizon.

Andrea

Andrea Faulds wrote:
> Hej allihopa,
> 
> Here's an RFC that's been lying in my drafts for uh… 26 months:
> 
> https://wiki.php.net/rfc/trailing_whitespace_numerics
> 
> I expect this should be an uncontroversial proposal, but maybe I'm 
> jinxing it there. I hope you all like it. :)
> 
> Thanks to Nikita for reminding me it existed and thus motivating me to 
> pick it up again. Also thanks to Nikita for suggesting a potential 
> follow-up RFC, and also making the “Saner string to number comparisons” 
> RFC, both providing additional impetus for me to finally clean this up 
> and put it to discussion.
> 
> Thanks!
> Andrea

---

## #104866 — Benjamin Morel — 2019-03-22T00:05:50+00:00

URL: https://externals.io/message/104590#104866

FWIW, I'd lean towards rejecting leading whitespace. That could be a good
BC break candidate for PHP 8!

Ben



> Hi again,
>
> I would proceed to a vote with this (and do intend to still), but the
> response from internals has been very quiet. Has it gotten lost among
> all the other things happening? :)
>
> I realise it's not a very exciting change of course, and there isn't
> much to discuss about it. It's pretty much a like it or hate it RFC with
> no particular nuance beyond that.
>
> One thing I do wonder about (given reaction on reddit and my personal
> general doubt) is how internals feels about whether we should take the
> opposite approach, that is, to break backwards-compatibility by
> rejecting *leading* whitespace in numeric strings instead. All the
> arguments in favour of this RFC's approach can also apply to that
> approach if you flip them around. The main reason to go for what I did
> so far is, well, it's harder to justify backwards-compatibility
> breakages. But I also initially came up with this when PHP 8 wasn't on
> the horizon.
>
> Andrea
>
> Andrea Faulds wrote:
> > […]

---

## #104867 — Marco Pivetta — 2019-03-22T00:07:13+00:00

URL: https://externals.io/message/104590#104867

> FWIW, I'd lean towards rejecting leading whitespace. That could be a good
> BC break candidate for PHP 8!
>
> Ben
>

Ack: leaning towards same thought

---

## #104959 — Theodore Brown — 2019-03-26T20:33:19+00:00

URL: https://externals.io/message/104590#104959

> > […]
>
>
> I would proceed to a vote with this (and do intend to still), but the
> response from internals has been very quiet. Has it gotten lost among
> all the other things happening? :)
> 
> I realise it's not a very exciting change of course, and there isn't
> much to discuss about it. It's pretty much a like it or hate it RFC with
> no particular nuance beyond that.
> 
> One thing I do wonder about (given reaction on reddit and my personal
> general doubt) is how internals feels about whether we should take the
> opposite approach, that is, to break backwards-compatibility by
> rejecting *leading* whitespace in numeric strings instead. All the
> arguments in favour of this RFC's approach can also apply to that
> approach if you flip them around. The main reason to go for what I did
> so far is, well, it's harder to justify backwards-compatibility
> breakages. But I also initially came up with this when PHP 8 wasn't on
> the horizon.


Hi Andrea, thanks for bringing forward this RFC. As a userland
developer, I definitely support fixing the current inconsistent
behavior one way or another:

```php
var_export("50" == " 50"); // true
var_export("50" == "50 "); // false
```

The current RFC removes the inconsistency by making both cases true.
This is better than nothing, but I think it would be better yet to
make both cases false. *Either way would be a BC break*, but from my
perspective it doesn't make sense to auto-trim whitespace when
comparing a string with another string.

Thanks,
-Theodore

---

## #104960 — Benjamin Morel — 2019-03-26T21:54:54+00:00

URL: https://externals.io/message/104590#104960

var_export("50" == " 50"); // true
>

I almost had a heart attack after running this code. I knew that PHP had a
few quirks when comparing different types, but I had long taken for granted
that comparing 2 *strings *performed a byte-by-byte comparison.

I also strongly support fixing the current quirk, rather than extending it.

Ben

---

## #104961 — Rowan Collins — 2019-03-26T22:28:06+00:00

URL: https://externals.io/message/104590#104961

> I almost had a heart attack after running this code. I knew that PHP had a
> few quirks when comparing different types, but I had long taken for granted
> that comparing 2 *strings *performed a byte-by-byte comparison.


You might want to have a stiff drink before reading through the examples 
in this RFC then: https://wiki.php.net/rfc/string_to_number_comparison

Regards,

-- 
Rowan Collins
[IMSoP]

---

## #104962 — Benjamin Morel — 2019-03-26T23:09:44+00:00

URL: https://externals.io/message/104590#104962

>
> You might want to have a stiff drink before reading through the examples
> in this RFC then: https://wiki.php.net/rfc/string_to_number_comparison


I've already browsed this RFC, I think it's a step in the right direction
and agree with it for the most part, with 2 exceptions:

- the one we're taking about here: disallow both leading AND trailing
whitespace in numeric strings
- I would also strongly suggest that 2 strings are always compared
byte-by-byte; IMO it does (*somehow*) make sense that 42 == "42.0", but it
DOES NOT make sense that "42" == "42.0". I mean, when you're comparing a
string to a number, surely you expect some kind of conversion to occur in
one way or another, but when comparing two strings, why this magic? Is that
because we want to enforce IF ($a == $b) AND ($b == $c) THEN ($a == $c) ?
If so, I would push this RFC one step further towards strictness and
suggest that:

```php
var_dump(42 == "42"); // true
var_dump(42 == "42.0"); // false
```

Which would meet this enforcement. I come across code that benefits from
automatic conversion between an integer and its string representation every
day, but have yet to encounter any code that would benefit from quirks such
as 42 == "42.0". Getting an integer as an int or as a string is no big
deal, but as soon as you start dealing with decimal strings (and floats),
you're asking for trouble if you're relying on such quirks if you ask me.

Also, may I suggest that these 2 RFCs be merged together?

https://wiki.php.net/rfc/string_to_number_comparison
https://wiki.php.net/rfc/trailing_whitespace_numerics

Ben



> 
> > […]
> a
> > […]
> granted
> > […]
>
>
> You might want to have a stiff drink before reading through the examples
> in this RFC then: https://wiki.php.net/rfc/string_to_number_comparison
>
> Regards,
>
> --
> Rowan Collins
> [IMSoP]

---

## #104963 — Claude Pache — 2019-03-27T10:28:17+00:00

URL: https://externals.io/message/104590#104963

> Le 27 mars 2019 à 00:09, Benjamin Morel <benjamin.morel@gmail.com> a écrit :
> 
> - I would also strongly suggest that 2 strings are always compared
> byte-by-byte; IMO it does (*somehow*) make sense that 42 == "42.0", but it
> DOES NOT make sense that "42" == "42.0". (...)

There are many ways to obtain numeric data in the form of a string from external sources (HTML form, database, CSV, ...) and even internal sources (bcmath, ...)—and note that, even when such numerical data may have a decimal part, it does *not* mean that they are imprecise. It is a well-known feature that you can work with such data without previously converting it explicitly to some number type. That works pretty well in PHP (well, except that you get intermittent obscure bugs when some third-party code in the same thread plays with `setlocale()`, but that's another story...) If we cease to compare numeric strings numerically, I expect that much code will begin to break more or less subtly, and will need to be thoroughly reviewed in order to be corrected. We must not change that.

The core issue is that “==” is fundamentally ambiguous: should it compare as number, as string, as reference, ...? If we were to redesign PHP from scratch, we may decide other semantics than the current one (e.g., use distinct operators for comparing numerically and textually, as does Perl). But where we are now, the best thing to do is to follow the common wisdom: If you want to compare strings as strings, just use “===”. Or strcmp.

—Claude

---

## #104964 — Benjamin Morel — 2019-03-27T13:29:47+00:00

URL: https://externals.io/message/104590#104964

> It is a well-known feature that you can work with such data without
previously converting it explicitly to some number type.

I'm not requesting to change this: if you're doing string("3.5") + int(7),
you should still get float(10.5). What I'm suggesting we change, is
comparison of numbers and decimal strings.

Thinking about this a bit more, what about following JavaScript semantics?
I find them really good in this respect:

- when comparing two strings, always use `strcmp()`:

var_dump("42" == "42.0"); // false
var_dump("3" < "23"); // false

- when comparing a number with a string, cast the string to a number, then
compare:

var_dump(3 < "23"); // true
var_dump("3" < 23); // true
var_dump(42 == "42"); // true
var_dump(42.0 == "42"); // true
var_dump(42.5 == "42.5"); // true
var_dump(42 == "42.0"); // true

- if the string does not represent a valid number, always return false:

var_dump(42 == "42x"); // false
var_dump(42 == "x42"); // false
var_dump(1 >= "abc"); // false
var_dump(1 < "abc"); // false

Note: JavaScript actually allows leading & trailing whitespace around the
number:

var_dump(42 == " 42 "); // true

On this last point, I'm not sure whether we should allow both leading &
trailing whitespace, or disallow both of them.
Other than that, I love the way they handle implicit conversions here. It's
so much more understandable, easy to remember, and less prone to errors IMO.

Ben



>
>
> > […]
> écrit :
> > […]
> it
> > […]
>
> There are many ways to obtain numeric data in the form of a string from
> external sources (HTML form, database, CSV, ...) and even internal sources
> (bcmath, ...)—and note that, even when such numerical data may have a
> decimal part, it does *not* mean that they are imprecise. It is a
> well-known feature that you can work with such data without previously
> converting it explicitly to some number type. That works pretty well in PHP
> (well, except that you get intermittent obscure bugs when some third-party
> code in the same thread plays with `setlocale()`, but that's another
> story...) If we cease to compare numeric strings numerically, I expect that
> much code will begin to break more or less subtly, and will need to be
> thoroughly reviewed in order to be corrected. We must not change that.
>
> The core issue is that “==” is fundamentally ambiguous: should it compare
> as number, as string, as reference, ...? If we were to redesign PHP from
> scratch, we may decide other semantics than the current one (e.g., use
> distinct operators for comparing numerically and textually, as does Perl).
> But where we are now, the best thing to do is to follow the common wisdom:
> If you want to compare strings as strings, just use “===”. Or strcmp.
>
> —Claude

---

## #104965 — G. P. B. — 2019-03-27T13:57:49+00:00

URL: https://externals.io/message/104590#104965

On Wed, 27 Mar 2019 at 14:30, Benjamin Morel <benjamin.morel@gmail.com>
wrote:

> > […]
> previously converting it explicitly to some number type.
>
> I'm not requesting to change this: if you're doing string("3.5") + int(7),
> you should still get float(10.5). What I'm suggesting we change, is
> comparison of numbers and decimal strings.
>
> Thinking about this a bit more, what about following JavaScript semantics?
> I find them really good in this respect:
>
> - when comparing two strings, always use `strcmp()`:
>
> var_dump("42" == "42.0"); // false
> var_dump("3" < "23"); // false
>
> - when comparing a number with a string, cast the string to a number, then
> compare:
>
> var_dump(3 < "23"); // true
> var_dump("3" < 23); // true
> var_dump(42 == "42"); // true
> var_dump(42.0 == "42"); // true
> var_dump(42.5 == "42.5"); // true
> var_dump(42 == "42.0"); // true
>
> - if the string does not represent a valid number, always return false:
>
> var_dump(42 == "42x"); // false
> var_dump(42 == "x42"); // false
> var_dump(1 >= "abc"); // false
> var_dump(1 < "abc"); // false
>
> Note: JavaScript actually allows leading & trailing whitespace around the
> number:
>
> var_dump(42 == " 42 "); // true
>
> On this last point, I'm not sure whether we should allow both leading &
> trailing whitespace, or disallow both of them.
> Other than that, I love the way they handle implicit conversions here. It's
> so much more understandable, easy to remember, and less prone to errors
> IMO.
>
> Ben
>

Isn't this the purpose of Nikic's Saner PHP String comparison RFC?
And in the discussion of this RFC the discussion of accepting trailings
whitespaces came into play.
As most people agree with changing it however trailling and leading spaces
should have the same behavior.

IMHO I think we should accept traillings spaces to make it more consistant
(and have a smaller BC break)
than banning leading and traillings spaces.
Because boths nikic's and Andrea's RFC go hand in hand.

And probably if both these RFC get accepted a new "stricter numerical
string comparison" RFC could see the light
banning leading and trailling whitespaces.

Just my two cents.

Best regards

George P. Banyard

---

## #104966 — Claude Pache — 2019-03-27T14:26:44+00:00

URL: https://externals.io/message/104590#104966

> Le 27 mars 2019 à 14:29, Benjamin Morel <benjamin.morel@gmail.com> a écrit :
> 
> Thinking about this a bit more, what about following JavaScript semantics? I find them really good in this respect:
> 

Arithmetic in JavaScript cannot be used reliably (in general) without converting your data to the proper type, because 1 + 1 gives you 11 if one of the 1s is encoded as string. PHP does a better job in that respect. As a result, there is good chance that people (it is at least the case for me) are more confident to rely (often implicitly) on automatic type conversion between strings and numbers when doing basic arithmetic in PHP than in JS, including for comparison of numeric values.

About leading/trailing whitespaces: usually, numbers encoded as strings do not contain them; so that it may be OK to consider strings containing them as non-numeric if we want.

—Claude

---

## #104967 — Benjamin Morel — 2019-03-27T15:14:09+00:00

URL: https://externals.io/message/104590#104967

>  Isn't this the purpose of Nikic's Saner PHP String comparison RFC?

It is, sorry I mixed the discussions here. As I said below, I think that
these RFCs would deserved to be merged.

> Because boths nikic's and Andrea's RFC go hand in hand.
> And probably if both these RFC get accepted a new "stricter numerical
string comparison" RFC could see the light
banning leading and trailling whitespace

I'd rather tackle the whole thing right now, as PHP 8 is a good opportunity
for whatever BC break this will cause. Another such opportunity will not
happen again before many years.

> Arithmetic in JavaScript cannot be used reliably (in general) without
converting your data to the proper type, because 1 + 1 gives you 11 if one
of the 1s is encoded as string. PHP does a better job in that respect

Sure, PHP has the huge advantage here to have a dedicated string
concatenation operator, removing any ambiguity around the + operator. This
does not mean that we cannot borrow their semantics for string/number
*comparison*!

Ben




>
> > […]
> écrit :
> > […]
> semantics? I find them really good in this respect:
> > […]
>
> Arithmetic in JavaScript cannot be used reliably (in general) without
> converting your data to the proper type, because 1 + 1 gives you 11 if one
> of the 1s is encoded as string. PHP does a better job in that respect. As a
> result, there is good chance that people (it is at least the case for me)
> are more confident to rely (often implicitly) on automatic type conversion
> between strings and numbers when doing basic arithmetic in PHP than in JS,
> including for comparison of numeric values.
>
> About leading/trailing whitespaces: usually, numbers encoded as strings do
> not contain them; so that it may be OK to consider strings containing them
> as non-numeric if we want.
>
> —Claude

---

## #104968 — Claude Pache — 2019-03-27T15:21:28+00:00

URL: https://externals.io/message/104590#104968

> Le 27 mars 2019 à 16:14, Benjamin Morel <benjamin.morel@gmail.com> a écrit :
> 
> 
> Sure, PHP has the huge advantage here to have a dedicated string concatenation operator, removing any ambiguity around the + operator. This does not mean that we cannot borrow their semantics for string/number comparison!

I was certainly not clear enough. I was thinking of basic handling of numeric values, including arithmetic operations *and* comparison operations.

—Claude

---

## #104969 — Benjamin Morel — 2019-03-27T15:31:19+00:00

URL: https://externals.io/message/104590#104969

>  I was certainly not clear enough. I was thinking of basic handling of
numeric values, including arithmetic operations *and* comparison operations.

I would be interested to have a real-life use case of when the current
number comparison capabilities of PHP make more sense than those, borrowed
from JS, suggested below?

Ben



>
>
> Le 27 mars 2019 à 16:14, Benjamin Morel <benjamin.morel@gmail.com> a
> écrit :
>
>
> Sure, PHP has the huge advantage here to have a dedicated string
> concatenation operator, removing any ambiguity around the + operator. This
> does not mean that we cannot borrow their semantics for string/number
> *comparison*!
>
>
> I was certainly not clear enough. I was thinking of basic handling of
> numeric values, including arithmetic operations *and* comparison operations.
>
> —Claude

---

## #104970 — Rowan Collins — 2019-03-27T16:19:31+00:00

URL: https://externals.io/message/104590#104970

On Tue, 26 Mar 2019 at 23:09, Benjamin Morel <benjamin.morel@gmail.com>
wrote:

> Also, may I suggest that these 2 RFCs be merged together?
>
> https://wiki.php.net/rfc/string_to_number_comparison
> https://wiki.php.net/rfc/trailing_whitespace_numerics
>


Although they are complementary, the two RFCs are tackling different
subjects, so should remain separate IMHO.

- Andrea's RFC is discussing the behaviour of parsing number strings in a
wide variety of contexts, including implicit and explicit casts.
- Nikita's RFC is discussing the specific case of comparisons, and applies
existing number-parsing rules to different contexts

I am certainly in favour of them both targeting PHP 8, possibly along with
additional "type juggling" changes, which could all be documented together,
but I think it would just be confusing to try to cover all the examples,
pros and cons, and voting options in one RFC.

Regards,
-- 
Rowan Collins
[IMSoP]
