Re: [RFC] Permit trailing whitespace in numeric strings

From: Date: Wed, 10 Apr 2019 08:32:46 +0000
Subject: Re: [RFC] Permit trailing whitespace in numeric strings
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-105191@lists.php.net to get a copy of this message
On 09/04/2019 14:27, Robert Hickman wrote:
Why? Wouldn't it be nice to align the behaviour of implicit and explicit casting, so that (int) "abc" throws a TypeError?
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

« previous php.internals (#105191) next »