I found a really weird behavior using SimpleXMLElement:
$xmlstr = '<?xml version="1.0" standalone="yes"?>
<movies>
<movie>
<title></title>
</movie>
</movies>';
$movies = new SimpleXMLElement($xmlstr);
$subelement = $movies->movie->title;
echo "Accesing normal: " . PHP_EOL;
echo var_dump($movies->movie->title) . PHP_EOL;
echo var_dump(empty($movies->movie->title)) . PHP_EOL;
echo "Accesing subelement: " . PHP_EOL;
echo var_dump($subelement) . PHP_EOL;
echo var_dump(empty($subelement)) . PHP_EOL;
The empty function is returning true when accessing the sub-elements using the main object, and is returning false when accessing the sub-elements using the variable containing the sub-element.
Why the behavior of empty is different if I pass the element in a variable than when I pass the element acceding from the main element?
Maybe empty is not the way to check for emptiness in this case, what is the correct way to check if the element is empty in a simpleXMLElement?