Hi, Marc Bennewitz wrote:
But I'm still curious why casting any non numeric string results in the valid number float(0) where there is a special value in floating point numbers declared to represent not a number values.
Because that's what C's strtod() does, and it's also what (int) does (because that's what C's strtol()) does.
There'd be an argument for using NaN, sure, but it wouldn't be consistent with the rest of the language. Generally, PHP assumes that a string with no leading digits has the number value of zero. This is true not only in (int) and (float) casts, but also in generic number coercion operations, like with +, -, *, / and **. To change the behaviour of (float) here would be to introduce a new inconsistency to PHP. If we go for NaN everywhere, then we introduce a different problem: (int) should return an integer, and the generic number coercions prefer giving you an integer rather than a float if appropriate. Producing a NaN here would mean you'd now get an int rather than a float, and would cause a cascading change in operation result types which could lead to previously working code now throwing lots of E_WARNINGs or TypeErrors (NaN is not accepted for integer type declarations in weak mode, and all floats are not accepted for integer type declarations in strict mode).
Thanks. -- Andrea Faulds https://ajf.me/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php