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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to