PHP Function Reference

PHP SimpleXMLIterator - valid() Method



The PHP SimpleXMLIterator::valid() method checks if the current element is valid after calls to SimpleXMLIterator::rewind() or SimpleXMLIterator::next() method.

Syntax

public SimpleXMLIterator::valid()

Parameters

No parameter is required.

Return Value

Returns true if the current element is valid, otherwise false.

Example:

The example below shows the usage of SimpleXMLIterator::valid() method.

<?php
$xml = <<<XML
<mail> 
  <To>John Smith</To>
  <From>Marry G.</From>
  <Subject>Happy Birthday</Subject>
  <body>Happy birthday. Live your life with smiles.</body>
</mail> 
XML;

$xmlIterator  = new SimpleXMLElement($xml);

var_dump($xmlIterator->valid()); //bool(false)

//rewind to first element
$xmlIterator->rewind(); 

var_dump($xmlIterator->valid()); //bool(true)

//advance to the next element
$xmlIterator->next(); 

var_dump($xmlIterator->valid()); //bool(true)
?>

The output of the above code will be:

bool(false)
bool(true)
bool(true)

❮ PHP SimpleXML Reference