Given that:
- XML data is always a string
- PHP type selection rules can be quite inconsistent.
... I would not rely on automatic juggling. Just determine the syntax that is more convenient for you, drop it into a line when reading and writing your own code for translation into boolean.
<?php function get_bool($value){ switch( strtolower($value) ){ case 'true': return true; case 'false': return false; default: return NULL; } } $data = '<?xml version="1.0"?> <data> <empty_tag/> <true_tag>true</true_tag> <false_tag>false</false_tag> </data> '; $xml = simplexml_load_string($data); var_dump( get_bool((string)$xml->empty_tag) ); var_dump( get_bool((string)$xml->true_tag) ); var_dump( get_bool((string)$xml->false_tag) );
Whatever it is, if you are interested in how to do the work (bool) , you mainly deal with objects, so this rule applies when casting to a logical one:
- SimpleXML objects created from empty tags: FALSE
- Each other value: TRUE
This means that you only get FALSE with <investigating /> (and TRUE with absolutely anything else). The trick I can come up with to expand the range of possible TRUE values is to double cast:
(bool)(string)$xml->investigating
Update: Remember to debug var_dump() . If you use echo , you will find that TRUE boolean distinguishes between the string '1' and FALSE to '' (empty string).
Álvaro González
source share