SimpleXMLIterator::valid()函數是PHP中的一個內置函數,用於檢查當前元素是否有效。
用法:
bool SimpleXMLIterator::valid( void )
參數:該函數不接受任何參數。
返回值:如果當前元素有效,則此函數返回TRUE,否則返回FALSE。
以下示例程序旨在說明PHP中的SimpleXMLIterator::valid()函數:
示例1:
<?php
// Store the xml element to variable
$xml = <<<XML
<organization>
<name>GeeksforGeeks</name>
<address>Noida India</address>
<contact>
<email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile>
</contact>
</organization>
XML;
$xmlIt = new SimpleXMLIterator($xml);
// Use rewind() function to rewind
// to the first element
$xmlIt->rewind();
// Display the result
var_dump($xmlIt->valid());
// Use next() function to move
// the next element
$xmlIt->next();
$xmlIt->next();
$xmlIt->next();
// Display the result
var_dump($xmlIt->valid());
?>
輸出:
bool(true) bool(false)
示例2:
<?php
// Store the xml element to variable
$xml = <<<XML
<organization>
<name>GeeksforGeeks</name>
<address>Noida India</address>
<contact>
<email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile>
</contact>
</organization>
XML;
$xmlIt = new SimpleXMLIterator($xml);
// Loop starts from first element of xml and
// run upto when elements are not valid
for( $xmlIt->rewind(); $xmlIt->valid(); $xmlIt->next() ) {
var_dump($xmlIt->key());
}
?>
輸出:
string(4) "name" string(7) "address" string(7) "contact"
參考: https://www.php.net/manual/en/simplexmliterator.valid.php
相關用法
- PHP SimpleXMLIterator next()用法及代碼示例
- PHP SimpleXMLIterator key()用法及代碼示例
- PHP SimpleXMLIterator current()用法及代碼示例
- PHP SimpleXMLIterator getChildren()用法及代碼示例
- PHP SimpleXMLIterator rewind()用法及代碼示例
- PHP SimpleXMLIterator hasChildren()用法及代碼示例
- PHP SplDoublyLinkedList valid()用法及代碼示例
- PHP AppendIterator valid()用法及代碼示例
- PHP SplObjectStorage valid()用法及代碼示例
- PHP SplFixedArray valid()用法及代碼示例
- PHP ArrayIterator valid()用法及代碼示例
- PHP DirectoryIterator valid()用法及代碼示例
- PHP Imagick valid()用法及代碼示例
注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | SimpleXMLIterator valid() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。