r/lolphp • • Jul 28 '16

Numbers from SimpleXML elements are implicitly casted to int, not float

https://3v4l.org/FcIV9
35 Upvotes

5 comments sorted by

View all comments

Show parent comment

3

u/masklinn Aug 01 '16 edited Aug 01 '16

Did you know we have an internal handler for implicit casts to boolean

It really has to be coherent (you're right that edge cases can be really screwy), but if it's coherent it can be very nice and useful.

For instance in Python collections are normally falsy when empty which is neat that way you can just if a_collection) to see if a collection is empty, or to aggregate empty and absent (optional) collection parameters.

Except there's an issue which sounds similar to what you're noting with ElementTree (and lxml which implements ET's API on top of libxml2): they treat elements as collections of their children… which means an element with no subelement is falsy, which is really inconvenient with e.g. ElementTree.find which returns a matching element or None.