php8.5
Home/ Manual/ simplexml / simplexmlelement/ SimpleXMLElement::valid

SimpleXMLElement::valid

PHP function Edit on GitHub ✎

Check whether the current element is valid

Description

SimpleXMLElement::valid(): bool
Warning

Prior to PHP 8.0, SimpleXMLElement::valid was only declared on the subclass SimpleXMLIterator.

This method checks if the current element is valid after calls to SimpleXMLElement::rewind or SimpleXMLElement::next.

Parameters Parameters

Return Values

Returns true if the current element is valid, otherwise false

Examples

Check whether the current element is valid

php
<?php
$xmlElement = new SimpleXMLElement('<books><book>SQL Basics</book></books>');

$xmlElement->rewind(); // rewind to the first element
echo var_dump($xmlElement->valid()); // bool(true)

$xmlElement->next(); // advance to the next element
echo var_dump($xmlElement->valid()); // bool(false) because there is only one element
?>

Source: reference/simplexml/simplexmlelement/valid.xml · from the official PHP manual (php/doc-en)