PHP | DOMNodeList count() Function
Last Updated :
17 Mar, 2020
The DOMNodeList::count() function is an inbuilt function in PHP which is used to get the number of nodes in the list.
Syntax:
int DOMNodeList::count( void )
Parameters: This function doesn’t accept any parameter.
Return Value: This function returns the number of nodes in the list.
Below examples illustrate the DOMNodeList::count() function in PHP:
Example 1:
<?php
$document = new DOMDocument();
$element = $document ->appendChild( new DOMElement( 'div' ));
$text1 = new DOMElement( 'h1' , 'GeeksforGeeks' );
$text2 = new DOMElement( 'h1' , 'Another GeeksforGeeks' );
$element ->appendChild( $text1 );
$element ->appendChild( $text2 );
$elements = $document ->getElementsByTagName( 'h1' );
echo $elements -> count ();
?>
|
Output:
2
Example 2:
<?php
$document = new DOMDocument();
$element = $document ->appendChild( new DOMElement( 'div' ));
$text1 = new DOMElement( 'h1' , 'GeeksforGeeks' );
$text2 = new DOMElement( 'h1' , 'Another GeeksforGeeks' );
$element ->appendChild( $text1 );
$element ->appendChild( $text2 );
$elements = $document ->getElementsByTagName( 'h1' );
echo 'Before removing: ' ;
echo $elements -> count () . "<br>" ;
$element ->removeChild( $text2 );
$elements = $document ->getElementsByTagName( 'h1' );
echo 'After removing: ' ;
echo $elements -> count ();
?>
|
Output:
Before removing: 2
After removing: 1
Reference: https://www.php.net/manual/en/domnodelist.count.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...