Search code examples
phpxpathdomxpath

PHP and Xpath (Xpath if node empty then set value as '0')


Hi all im struggling with an XPath query, I want the query to return a '0' value when it has found nothing. Here is my code so far:

$query = $xpathvar->query('//DataStatistics/report'); foreach($query as $result){ echo "$result->length";

It just returns blank at the moment, any suggestions would be greatly appreciated??


Solution

  • You have to get the length of the $query itself (if there's no length, the loop never runs):

    $query = $xpathvar->query('//DataStatistics/report');
    echo $query->length;