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 »
The valid() function checks whether the current element is valid after a call to rewind() or next().
SimpleXMLIterator::valid()
Return Value: | TRUE if element is valid. FALSE otherwise |
---|---|
PHP Version: | 5.0+ |
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 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!