PHP valid() Function
Example
Check whether the current element is valid after a call to rewind() and next():
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Do not forget me this weekend!</body>
</note>
XML;
$xml = new SimpleXMLIterator($note);
// rewind to the first element
$xml->rewind();
// check if valid
var_dump($xml->valid());
// move to the next element
$xml->next();
// check if valid
var_dump($xml->valid());
?>
Run Example »
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Do not forget me this weekend!</body>
</note>
XML;
$xml = new SimpleXMLIterator($note);
// rewind to the first element
$xml->rewind();
// check if valid
var_dump($xml->valid());
// move to the next element
$xml->next();
// check if valid
var_dump($xml->valid());
?>
Definition and Usage
The valid() function checks whether the current element is valid after a call to rewind() or next().
Syntax
SimpleXMLIterator::valid()
Technical Details
| Return Value: | TRUE if element is valid. FALSE otherwise |
|---|---|
| PHP Version: | 5.0+ |
More Examples
Example
Check whether the current element is valid after a call to rewind() and next():
<?php
$xml = new SimpleXMLIterator('<books><book>Learn PHP</book></books>');
// rewind to the first element
$xml->rewind();
// check if valid
var_dump($xml->valid());
// move to the next element
$xml->next();
// check if valid - will be bool(false) because there is only one element
var_dump($xml->valid());
?>
Run Example »
$xml = new SimpleXMLIterator('<books><book>Learn PHP</book></books>');
// rewind to the first element
$xml->rewind();
// check if valid
var_dump($xml->valid());
// move to the next element
$xml->next();
// check if valid - will be bool(false) because there is only one element
var_dump($xml->valid());
?>
❮ PHP SimpleXML Reference