# [RFC] Permit trailing whitespace in numeric strings

- Thread: https://externals.io/message/104594
- Original archive: https://news-web.php.net/php.internals/104594
- Started by Markus Fischer on 2019-03-06T06:39:43+00:00
- Messages: 19
- 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

---

## #104594 — Markus Fischer — 2019-03-06T06:39:43+00:00

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

Hello,


> 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.

Hmm, my first reaction would be the opposite.

For all the changes which happened during the years, PHP got stricter; 
this would make it more lax.

I know this is totally not the goal of the RFC, by my feeling is if we 
would change something (ignoring BC break for a sec) that it would be to 
actually _remove_ support for leading whitespace ?‍♀️

- Markus

---

## #104598 — Nikita Popov — 2019-03-06T08:58:39+00:00

URL: https://externals.io/message/104594#104598

> Hello,
>
> 
> > […]
>
> Hmm, my first reaction would be the opposite.
>
> For all the changes which happened during the years, PHP got stricter;
> this would make it more lax.
>
> I know this is totally not the goal of the RFC, by my feeling is if we
> would change something (ignoring BC break for a sec) that it would be to
> actually _remove_ support for leading whitespace 🤷‍♀️
>
> - Markus
>

I'm always a fan of making things stricter, but think that in this
particular case there are some additional considerations we should keep in
mind.

1. What is more important to me here than strictness is consistency. Either
both "   123" and "123   " are numeric, or neither are. Making "123    "
numeric is a change we can easily do, because it makes the numeric string
definition more permissive and is thus mostly backwards compatible. Doing
the reverse change is certainly not compatible and will be a much harder
sell.

2. I believe that a large part of the motivation here is that by making the
numeric string definition slightly more lax (in a consistent manner), we
can make *other* things more strict, because this essentially eliminates
the only "somewhat reasonable" case of trailing characters. The RFC already
mentions two of them:

a) We can hard reject "123foo" inputs to "int" arguments (and some other
places). Currently this is allowed with a notice. I think if we resolve the
trailing whitespace question, then there cannot be any reasonable
opposition to this change.
b) My own RFC on number to string comparisons would benefit from this. From
initial testing it has surprisingly little impact, but one of the few cases
that turned up was this comparison with a string that had trailing
whitespace.

Personally I think both of those changes are a lot more valuable than a
stricter numeric string definition without leading/trailing whitespace.

Regards,
Nikita

---

## #105065 — Andrea Faulds — 2019-04-03T23:15:44+00:00

URL: https://externals.io/message/104594#105065

Nikita Popov wrote:
> I'm always a fan of making things stricter, but think that in this
> particular case there are some additional considerations we should keep in
> mind.
> 
> 1. What is more important to me here than strictness is consistency. Either
> both "   123" and "123   " are numeric, or neither are. Making "123    "
> numeric is a change we can easily do, because it makes the numeric string
> definition more permissive and is thus mostly backwards compatible. Doing
> the reverse change is certainly not compatible and will be a much harder
> sell.
> 
> 2. I believe that a large part of the motivation here is that by making the
> numeric string definition slightly more lax (in a consistent manner), we
> can make *other* things more strict, because this essentially eliminates
> the only "somewhat reasonable" case of trailing characters. The RFC already
> mentions two of them:
> 
> a) We can hard reject "123foo" inputs to "int" arguments (and some other
> places). Currently this is allowed with a notice. I think if we resolve the
> trailing whitespace question, then there cannot be any reasonable
> opposition to this change.
> b) My own RFC on number to string comparisons would benefit from this. From
> initial testing it has surprisingly little impact, but one of the few cases
> that turned up was this comparison with a string that had trailing
> whitespace.
> 
> Personally I think both of those changes are a lot more valuable than a
> stricter numeric string definition without leading/trailing whitespace.

I'm kinda unsure how to go forward because of these points. I would like 
to see improved comparisons, and I would like to see the end of the 
“non-well-formed” numeric string, and I think this whitespace RFC could 
be helpful to both. But I can't see the future, I don't know whether 
people will vote for removing leading or permitting traiing whitespace 
and whether or not they will be influenced by or this will influence 
opinion on the further improvements. ¯\_(ツ)_/¯

I'm torn between:

* Vote on allowing trailing whitespace
* Vote on disallowing leading whitespace
* Vote on which of those two approaches to go for
* Trying to bundle everything together and voting on it as a package.

I'm probably thinking too strategically.

Andrea

---

## #105068 — Benjamin Morel — 2019-04-04T08:57:31+00:00

URL: https://externals.io/message/104594#105068

What about going forward with the trailing whitespace RFC right now, but
ask to vote between 3 options?

- NO - leave as it is
- YES - allow trailing whitespace
- YES - disallow leading whitespace

And then proceeding with the string to number comparison RFC?

Ben



> Nikita Popov wrote:
> > […]
> in
> > […]
> Either
> > […]
> the
> > […]
> already
> > […]
> the
> > […]
> From
> > […]
> cases
> > […]
>
> I'm kinda unsure how to go forward because of these points. I would like
> to see improved comparisons, and I would like to see the end of the
> “non-well-formed” numeric string, and I think this whitespace RFC could
> be helpful to both. But I can't see the future, I don't know whether
> people will vote for removing leading or permitting traiing whitespace
> and whether or not they will be influenced by or this will influence
> opinion on the further improvements. ¯\_(ツ)_/¯
>
> I'm torn between:
>
> * Vote on allowing trailing whitespace
> * Vote on disallowing leading whitespace
> * Vote on which of those two approaches to go for
> * Trying to bundle everything together and voting on it as a package.
>
> I'm probably thinking too strategically.
>
> Andrea

---

## #105070 — Lester Caine — 2019-04-04T10:49:31+00:00

URL: https://externals.io/message/104594#105070

> 1. What is more important to me here than strictness is consistency. Either
> both "   123" and "123   " are numeric, or neither are. Making "123    "
> numeric is a change we can easily do, because it makes the numeric string
> definition more permissive and is thus mostly backwards compatible. Doing
> the reverse change is certainly not compatible and will be a much harder
> sell.

I think it may be worth pointing out here the distinction at least with 
Firebird and I thought with other databases? A CHAR field is always 
returned padded with spaces to it's length, while a VARCHAR field is 
always trimmed to the last non space character. There are often reasons 
for using CHAR although many of those are probably historic, but 
trimming them without proper consideration is less than ideal. Returning 
historic material where numeric data is right justified in a CHAR field 
is equally valid.

Handling these has always required care and it would be preferable that 
both returned the same result without having to trim and perhaps 
creating problems when the padded version needs to be returned ...

-- 
Lester Caine - G8HFL
-----------------------------
Contact - https://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - https://lsces.co.uk
EnquirySolve - https://enquirysolve.com/
Model Engineers Digital Workshop - https://medw.co.uk
Rainbow Digital Media - https://rainbowdigitalmedia.co.uk

---

## #105074 — M. W. Moe — 2019-04-04T15:05:38+00:00

URL: https://externals.io/message/104594#105074

Hello,

what about exposing a strict keyword option or a php ini option?

- NO - leave as it is                            default
- YES - allow trailing whitespace       punks
- YES - disallow leading whitespace sane people

On Thu, Apr 4, 2019 at 1:57 AM Benjamin Morel <benjamin.morel@gmail.com>
wrote:

> What about going forward with the trailing whitespace RFC right now, but
> ask to vote between 3 options?
>
> - NO - leave as it is
> - YES - allow trailing whitespace
> - YES - disallow leading whitespace
>
> And then proceeding with the string to number comparison RFC?
>
> Ben
>
> 
>
> > […]
> "
> > […]
> string
> > […]
> Doing
> > […]
> harder
> > […]
> we
> > […]
> eliminates
> > […]
> other
> > […]

---

## #105077 — Benjamin Morel — 2019-04-04T16:06:16+00:00

URL: https://externals.io/message/104594#105077

>
> what about exposing a strict keyword option or a php ini option?


There is a trend right now towards avoiding the language to be dependent on
php.ini options. I'm on board with this, and would personally strongly
discourage introducing such an option, and enforce one of these options for
everyone instead.



> Hello,
>
> what about exposing a strict keyword option or a php ini option?
>
> - NO - leave as it is                            default
> - YES - allow trailing whitespace       punks
> - YES - disallow leading whitespace sane people
>
> On Thu, Apr 4, 2019 at 1:57 AM Benjamin Morel <benjamin.morel@gmail.com>
> wrote:
>
> > […]

---

## #105080 — M. W. Moe — 2019-04-04T16:15:30+00:00

URL: https://externals.io/message/104594#105080

Hello,

Yes and No php has an existing echo-system of extensions which could not
work
without this ini model; therefor it's fashion trend which is in my opinion
unrealistic or you
are ready to break existing infrastructures.

Enforcing  the right behavior (third one) will break existing code big
time; hence at some point
you need to go with a transitional state; it does not say that one day
you'r not gonna retire it
and finally enforcing only one ((the right behavior (third one))).




On Thu, Apr 4, 2019 at 9:06 AM Benjamin Morel <benjamin.morel@gmail.com>
wrote:

> what about exposing a strict keyword option or a php ini option?
>
>
> There is a trend right now towards avoiding the language to be dependent
> on php.ini options. I'm on board with this, and would personally strongly
> discourage introducing such an option, and enforce one of these options for
> everyone instead.
>
> 
>
> > […]

---

## #105167 — Nikita Popov — 2019-04-09T10:47:49+00:00

URL: https://externals.io/message/104594#105167

> Nikita Popov wrote:
> > […]
> in
> > […]
> Either
> > […]
> the
> > […]
> already
> > […]
> the
> > […]
> From
> > […]
> cases
> > […]
>
> I'm kinda unsure how to go forward because of these points. I would like
> to see improved comparisons, and I would like to see the end of the
> “non-well-formed” numeric string, and I think this whitespace RFC could
> be helpful to both. But I can't see the future, I don't know whether
> people will vote for removing leading or permitting traiing whitespace
> and whether or not they will be influenced by or this will influence
> opinion on the further improvements. ¯\_(ツ)_/¯
>
> I'm torn between:
>
> * Vote on allowing trailing whitespace
> * Vote on disallowing leading whitespace
> * Vote on which of those two approaches to go for
> * Trying to bundle everything together and voting on it as a package.
>
> I'm probably thinking too strategically.
>

Given the response on the mailing list (and also other places like Reddit),
it seems like people feel pretty strongly that it's better to drop support
for leading whitespace than add support for trailing whitespace. If we do
this, I think we should couple this change with the removal of "non
well-formed numeric strings", because they are so closely related (one
change would forbid leading whitespace and the other trailing characters).

One possible course of action would be:

a) In PHP 7.4 throw a deprecation warning in is_numeric_string if there is
leading whitespace (always).
b) In PHP 7.4 throw a deprecation warning in is_numeric_string if there are
trailing characters in mode 1 (mode -1 already throws a notice and 0
already treats as non-numeric).
b) In PHP 8.0 treat leading whitespace as non-numeric (always).
c) In PHP 8.0 treat trailing characters as non-numeric (always),  and
remove the non well-formed distinction (mode -1).

Notably this also affects (int) behavior in that (int) "   42" will be 0
and (int) "42xyz" will be 0.

A less aggressive alternative would be:

a) In PHP 7.4 throw a deprecation warning in is_numeric_string if there is
leading whitespace (unless mode is 1).
b) In PHP 8.0 treat leading whitespace as non-numeric (unless mode is 1).
c) In PHP 8.0 treat leading characters as non-numeric (unless mode is 1).
Remove non well-formed distinction (mode -1).

This would keep the behavior of (int) as-is and only affect implement
numeric string checks.

This discussion how mostly been around the implicit cases, what do people
think about the desired behavior of (int)?

Regards,
Nikita

---

## #105168 — Derick Rethans — 2019-04-09T10:57:05+00:00

URL: https://externals.io/message/104594#105168

> 
> 
> > […]
> 
> Given the response on the mailing list (and also other places like Reddit),
> it seems like people feel pretty strongly that it's better to drop support
> for leading whitespace than add support for trailing whitespace. If we do
> this, I think we should couple this change with the removal of "non
> well-formed numeric strings", because they are so closely related (one
> change would forbid leading whitespace and the other trailing characters).
> 
> One possible course of action would be:
> 
> a) In PHP 7.4 throw a deprecation warning in is_numeric_string if there is
> leading whitespace (always).
> b) In PHP 7.4 throw a deprecation warning in is_numeric_string if there are
> trailing characters in mode 1 (mode -1 already throws a notice and 0
> already treats as non-numeric).
> b) In PHP 8.0 treat leading whitespace as non-numeric (always).
> c) In PHP 8.0 treat trailing characters as non-numeric (always),  and
> remove the non well-formed distinction (mode -1).
> 
> Notably this also affects (int) behavior in that (int) "   42" will be 0
> and (int) "42xyz" will be 0.
> 
> A less aggressive alternative would be:
> 
> a) In PHP 7.4 throw a deprecation warning in is_numeric_string if there is
> leading whitespace (unless mode is 1).
> b) In PHP 8.0 treat leading whitespace as non-numeric (unless mode is 1).
> c) In PHP 8.0 treat leading characters as non-numeric (unless mode is 1).
> Remove non well-formed distinction (mode -1).
> 
> This would keep the behavior of (int) as-is and only affect implement
> numeric string checks.
> 
> This discussion how mostly been around the implicit cases, what do people
> think about the desired behavior of (int)?

I think there should be no difference in behaviour between implicit and 
explicit cases.

cheers,
Derick
-- 
https://derickrethans.nl | https://xdebug.org | https://dram.io
Like Xdebug? Consider a donation: https://xdebug.org/donate.php,
or become my Patron: https://www.patreon.com/derickr
twitter: @derickr and @xdebug

---

## #105170 — Nikita Popov — 2019-04-09T11:06:01+00:00

URL: https://externals.io/message/104594#105170

> 
>
> > […]
> like
> > […]
> Reddit),
> > […]
> support
> > […]
> characters).
> > […]
> is
> > […]
> are
> > […]
> is
> > […]
>
> I think there should be no difference in behaviour between implicit and
> explicit cases.


I should probably clarify what I mean by explicit and implicit here. By
explicit I mean anything using (int) casts or doing so internally
(implicitly ^^) -- this *must* produce an integer in some way and does not
have the option of rejecting the input. By implicit I mean other places
checking for numeric strings, such as "int" parameters. These *do* have the
option of rejecting the input. Both cannot work the same way due to the
different constraints.

So to rephrase my question: While I think there is a consensus that
"123xyz" and "   123" should not be accepted by an "int" parameter, it is
not clear to me that there is also a consensus that (int) "123xyz" and
(int) "   123" should result in 0 rather than 123.

Regards,
Nikita

---

## #105171 — Derick Rethans — 2019-04-09T11:10:56+00:00

URL: https://externals.io/message/104594#105171

> 
> 
> > […]
> 
> So to rephrase my question: While I think there is a consensus that 
> "123xyz" and "  123" should not be accepted by an "int" parameter, it 
> is not clear to me that there is also a consensus that (int) "123xyz" 
> and (int) "  123" should result in 0 rather than 123.

I meant that (int) should accept the same data as "int" parameters, and 
hence, the result of (int) "  123" should be 0.

cheers,
Derick

-- 
https://derickrethans.nl | https://xdebug.org | https://dram.io
Like Xdebug? Consider a donation: https://xdebug.org/donate.php,
or become my Patron: https://www.patreon.com/derickr
twitter: @derickr and @xdebug

---

## #105173 — Benjamin Morel — 2019-04-09T11:55:18+00:00

URL: https://externals.io/message/104594#105173

>
> I should probably clarify what I mean by explicit and implicit here. By
> explicit I mean anything using (int) casts or doing so internally
> (implicitly ^^) -- this *must* produce an integer in some way and does not
> have the option of rejecting the input. By implicit I mean other places
> checking for numeric strings, such as "int" parameters. These *do* have the
> option of rejecting the input. Both cannot work the same way due to the
> different constraints.


Why? Wouldn't it be nice to align the behaviour of implicit and explicit
casting, so that (int) "abc" throws a TypeError?

Ben



> 
>
> > […]
> could
> > […]
> whitespace
> > […]
> do
> > […]
> 0
> > […]
> 1).
> > […]
> 1).
> > […]
> people
> > […]
>
>
> I should probably clarify what I mean by explicit and implicit here. By
> explicit I mean anything using (int) casts or doing so internally
> (implicitly ^^) -- this *must* produce an integer in some way and does not
> have the option of rejecting the input. By implicit I mean other places
> checking for numeric strings, such as "int" parameters. These *do* have the
> option of rejecting the input. Both cannot work the same way due to the
> different constraints.
>
> So to rephrase my question: While I think there is a consensus that
> "123xyz" and "   123" should not be accepted by an "int" parameter, it is
> not clear to me that there is also a consensus that (int) "123xyz" and
> (int) "   123" should result in 0 rather than 123.
>
> Regards,
> Nikita

---

## #105174 — Robert Hickman — 2019-04-09T13:27:28+00:00

URL: https://externals.io/message/104594#105174

> Why? Wouldn't it be nice to align the behaviour of implicit and explicit
> casting, so that (int) "abc" throws a TypeError?
>

I agree with this. It would be odd if they behaved differently.

---

## #105191 — Mark Randall — 2019-04-10T08:32:46+00:00

URL: https://externals.io/message/104594#105191

> > […]

I'm somewhat split on this one.

On the one hand, if I make an explicit cast, my intention is clear, make 
the best attempt to convert this string into a number. Trimming 
whitespace would be a natural consequence of that.

On the other hand, I'm a stickler for consistency.

On the balance of things, I would most certainly like to see leading and 
trailing whitespace render a string ineligible to be implicitly 
converted, but I'd be satisfied with (int)" 123 " converting without error.

On the topic of errorsfor explicit casts I'd be more inclined to also 
introduce (?int) and in the event the value cannot be converted, return 
null.

As long as the precidence worked out, it would allow:

$output = (?int)$input ?? $default;

--
Mark Randall

---

## #105199 — Robert Hickman — 2019-04-10T09:36:07+00:00

URL: https://externals.io/message/104594#105199

> On the balance of things, I would most certainly like to see leading and
> trailing whitespace render a string ineligible to be implicitly
> converted, but I'd be satisfied with (int)" 123 " converting without error.
>

Given that PHP seems to be gradually moving towards more type
strictness, would it make more sense to move this functionality to a
'parse_numeric_string()' function? Js has parseInt() and parseFloat()
with similar functionality.

---

## #105200 — Benjamin Morel — 2019-04-10T09:37:30+00:00

URL: https://externals.io/message/104594#105200

>
> I'm somewhat split on this one.
> On the one hand, if I make an explicit cast, my intention is clear, make
> the best attempt to convert this string into a number. Trimming
> whitespace would be a natural consequence of that.
> On the other hand, I'm a stickler for consistency.
> On the balance of things, I would most certainly like to see leading and
> trailing whitespace render a string ineligible to be implicitly
> converted, but I'd be satisfied with (int)" 123 " converting without error.
>

I'm all for consistency between `(int)` casting, `(int $x)` function
parameter and `: int` return type, *even* if this justifies allowing
leading and trailing whitespace around numbers, if this helps adoption.
As long as we no longer allow `(int) "abc"` to return `0`, I'm game.

On the topic of errorsfor explicit casts I'd be more inclined to also
> introduce (?int) and in the event the value cannot be converted, return
> null.


If (?int) casting is introduced, I would only return null if the original
value is null. Otherwise, throw an TypeError.
This is the only consistent behaviour with scalar type hints and return
types.

Ben

---

## #105268 — Claude Pache — 2019-04-13T17:34:35+00:00

URL: https://externals.io/message/104594#105268

> Le 9 avr. 2019 à 12:47, Nikita Popov <nikita.ppv@gmail.com> a écrit :
> 
> 
> 
> > […]
> 
> Given the response on the mailing list (and also other places like Reddit),
> it seems like people feel pretty strongly that it's better to drop support
> for leading whitespace than add support for trailing whitespace. If we do
> this, I think we should couple this change with the removal of "non
> well-formed numeric strings", because they are so closely related (one
> change would forbid leading whitespace and the other trailing characters).
> 
> One possible course of action would be:
> 
> a) In PHP 7.4 throw a deprecation warning in is_numeric_string if there is
> leading whitespace (always).
> b) In PHP 7.4 throw a deprecation warning in is_numeric_string if there are
> trailing characters in mode 1 (mode -1 already throws a notice and 0
> already treats as non-numeric).
> b) In PHP 8.0 treat leading whitespace as non-numeric (always).
> c) In PHP 8.0 treat trailing characters as non-numeric (always),  and
> remove the non well-formed distinction (mode -1).
> 
> Notably this also affects (int) behavior in that (int) "   42" will be 0
> and (int) "42xyz" will be 0.
> 
> A less aggressive alternative would be:
> 
> a) In PHP 7.4 throw a deprecation warning in is_numeric_string if there is
> leading whitespace (unless mode is 1).
> b) In PHP 8.0 treat leading whitespace as non-numeric (unless mode is 1).
> c) In PHP 8.0 treat leading characters as non-numeric (unless mode is 1).
> Remove non well-formed distinction (mode -1).
> 
> This would keep the behavior of (int) as-is and only affect implement
> numeric string checks.
> 
> This discussion how mostly been around the implicit cases, what do people
> think about the desired behavior of (int)?
> 
> Regards,
> Nikita

I think that, whatever change we make around ”well-formed numeric string” (those treated as numeric by the comparison operator, etc.), the casting operation itself (implicit as in `$a + $b` or explicit as in `(float) $a`) must keep the same semantics, except maybe with additional notices and/or warnings for the implicit case. People depend on that semantics, and we must not break their code, except maybe we could force them to be explicit (using (float)) if we have sufficiently good reason for that. I’ll give use cases further below.

To keep the things simple, I suggest to classify the strings in three categories: 

1. Well-formed numeric strings; those, and those only, are considered as valid numeric values for the sake of comparison, of typed parameters, of `is_numeric()`, etc. Let’s be strict here, and let’s not permit whitespaces.

2. Loose numeric strings. Explicit casting must continue to work as today, since people expect that semantics (see first use case below); ditto for implicit casting but with an additional notice or warning. Those strings are of the form: 
    * optional whitespaces, followed by
    * well-formed numeric string, followed by
    * optional garbage.

3. Non-numeric strings. Explicit casting must continue to work as today, as people may depend on that (see second use case below). A notice may be acceptable if we don’t bother people using the evil @ operator in more places than today. Ditto for implicit casting, but with an additional warning, unless we decide to harshly throw a Throwable if we have a good reason for that.

------

Here is a real-world use case, where I want the casting from “loose numeric string” to float to continue to work as today:

I have a CSS stylesheet. Because I find it more practical, I prefer to encode the colours in the hwb (hue-whiteness-blackness) format: 

```css
:root {
    --red-hue: 350;
}
.alert {
    color: hwb(var(--red-hue),  6% , 12%);
}
```

However browsers do not (yet) understand hwb; so I must serve them as hsl (hue-saturation-lightness):

```css
.alert {
    color: hsl(var(--red-hue), 87.2%, 47%);
}
```

The transformation between hwb() and hsl() is quite simple, and I use PHP as a preprocessor (on my dev machine) for that purpose. The algorithm does not need to include a true CSS parser, as I know in advance that the `hwb()` expressions I’ve written myself contain only either literal numbers or literal percentages for the “whiteness” and “blackness” components, and no comma in the “hue” component:

* find an occurence of "hwb(";
* read characters up to the next ",": this is the “hue” component;
* read characters up to the next ",": this is the “whiteness” component;
* read next characters up to the next ")": this is the “blackness” component;
* if the “whiteness” (respectively the “darkness”) component contains the character "%", it is a percentage: divide it by 100;
* calculate “saturation” and “lightness” from “whiteness” and “blackness”;
* format and assemble the “hue”, “saturation” and “lightness” components into a "hsl(...)" expression.

If you have followed the algorithm, you should have noticed that at one point I have the values:

$whiteness === "  6% ";
$darkness === " 12%";

which are exactly of the format “loose numeric string” (whitespaces, followed by well-formed numeric string, followed by garbage) described above; and that I straightforwardly treat as numeric (knowing that PHP permits that), with or without explicitly casting depending on how much I am bothered by the “not well-formed numeric value” notice.

----

Here is a use case where I want a string-to-float casting that never fail (and to not change semantics either):

I have config parameters stored in a database, with values in the form of string. Some of those parameters are supposed to be numeric; for example, two of them may represent the offset (in millimetres) of the company logo, on printed material, relatively to a predefined position. A missing value (either the empty string or null) is treated as zero. An invalid value (such as "foo") is also happily treated as zero, as I don’t bother to report the error at this point: it is straightforwardly seen whether the logo is correctly placed or not. For that purpose, I’m relying on the fact that the explicit string-to-numeric casting operation is infallible.

—Claude

---

## #105270 — Mark Randall — 2019-04-14T13:53:08+00:00

URL: https://externals.io/message/104594#105270

A thought -

In the event that explicit casting specifically does get tightened up, 
what will become the suggested method for making a best-effort 
conversion to an integer?

Personally I'm in favour of explicit casts being a bit more forgiving, 
but in the event they're not, what will replace it as the recommended 
inbuilt method that handles strings with whitespace and trailing junk?

Will there be one?

I'm aware that the most common IDEs suggest using (int) instead of intval.

--
Mark Randall
