(沒有版本資訊,可能僅在 Git 中)
SimpleXMLElement::hasChildren — 檢查目前元素是否有子元素
在 PHP 8.0 之前,SimpleXMLElement::hasChildren() 僅在子類別 SimpleXMLIterator 上宣告。
此方法檢查目前的 SimpleXMLElement 元素是否有子元素。
此函式沒有參數。
範例 #1 檢查目前的元素是否有子元素
<?php
$xml = <<<XML
<books>
<book>
<title>PHP 基礎</title>
<author>Jim Smith</author>
</book>
<book>XML 基礎</book>
</books>
XML;
$xmlElement = new SimpleXMLElement($xml);
for ($xmlElement->rewind(); $xmlElement->valid(); $xmlElement->next()) {
if ($xmlElement->hasChildren()) {
var_dump($xmlElement->current());
}
}
?>
上述範例將輸出
object(SimpleXMLElement)#2 (2) { ["title"]=> string(10) "PHP Basics" ["author"]=> string(9) "Jim Smith" }