SimpleXMLElement::hasChildren

(PHP 8)

SimpleXMLElement::hasChildrenVerifica si el elemento actual tiene subelementos

Descripción

public SimpleXMLElement::hasChildren(): bool
Advertencia

Antes de PHP 8.0, SimpleXMLElement::hasChildren() solo estaba declarada en la subclase SimpleXMLIterator.

Este método verifica si el objeto actual SimpleXMLElement tiene subelementos.

Parámetros

Esta función no tiene parámetros.

Valores devueltos

true si la entrada actual tiene subelementos, false en caso contrario.

Ejemplos

Ejemplo #1 Verifica si un elemento tiene subelementos

<?php
$xml
= <<<XML
<books>
<book>
<title>PHP Basics</title>
<author>Jim Smith</author>
</book>
<book>XML basics</book>
</books>
XML;

$xmlElement = new SimpleXMLElement($xml);
for (
$xmlElement->rewind(); $xmlElement->valid(); $xmlElement->next()) {
if (
$xmlElement->hasChildren()) {
var_dump($xmlElement->current());
}
}
?>

El resultado del ejemplo sería:

object(SimpleXMLElement)#2 (2) {
  ["title"]=>
  string(10) "PHP Basics"
  ["author"]=>
  string(9) "Jim Smith"
}

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top