Hi,

Marc Bennewitz wrote:
It's totally true that on casting non numeric string to integer will
result in 0 but that's a different type even if it's also a numeric type.

 From my understanding PHP tries to cast from one type to another by
loosing as less information as possible. In this case I see a non
numeric string that gets casted to a float will loose exactly this
information that it's "Not a Number" where it would be possible to keep
this information. On casting this to int there is of course no
possibility to keep it but I don't see where it's inconsistent as it's a
different type used for different use-cases.

(int) and (float) are lossy and have always been so. If you want PHP to complain when you give it something non-numeric, rather than coerce to 0, there are alternatives.

Having mathematical operations with non numeric strings should result in
the same behavior as NaN will result in NaN which makes sense and I
don't see where it will throw lots on WARNINGS or TypeErrors as it's
still a float.

Because previously it would be an integer. "foo" + "bar" currently gives an integer 0. Now it would give you a float NaN. That has potential knock-on effects.

Thanks.

--
Andrea Faulds
https://ajf.me/

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

Reply via email to