note 102866 added to class.domnodelist
| From: | vector dot kerr at gmail dot com | Date: | Fri, 11 Mar 2011 06:41:43 +0000 |
| Subject: | note 102866 added to class.domnodelist | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177784@lists.php.net to get a copy of this message | ||
You may also iterate over a DOMNodeList which is useful if you don't like creating extra
variables.
<?PHP
foreach( $nodelist as $index => $node ) { /* ... */ }
?>
@geompse:
The reason that this ...
<?PHP
for( $pos=0; $pos<$nodelist->length; $pos++ ) { /* ... */ }
?>
... takes so long is because each time the loop progresses and the conditions are checked, the
length of the node list must be recalculated.
Your alternative...
<?PHP
$length = $nodelist->length;
for( $i = 0; $i < $length; $i++ ) { /* ... */ }
?>
... stores the value once, which is a more appropriate approach.
----
Server IP: 69.147.83.197
Probable Submitter: 139.168.2.92
----
Manual Page -- http://www.php.net/manual/en/class.domnodelist.php
Edit -- https://master.php.net/note/edit/102866
Del: integrated -- https://master.php.net/note/delete/102866/integrated
Del: useless -- https://master.php.net/note/delete/102866/useless
Del: bad code -- https://master.php.net/note/delete/102866/bad+code
Del: spam -- https://master.php.net/note/delete/102866/spam
Del: non-english -- https://master.php.net/note/delete/102866/non-english
Del: in docs -- https://master.php.net/note/delete/102866/in+docs
Del: other reasons-- https://master.php.net/note/delete/102866
Reject -- https://master.php.net/note/reject/102866
Search -- https://master.php.net/manage/user-notes.php