note 50900 added to control-structures.while
| From: | php-general at lists dot php dot net | Date: | Sun, 13 Mar 2005 17:54:47 +0000 |
| Subject: | note 50900 added to control-structures.while | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86364@lists.php.net to get a copy of this message | ||
virtualjosh at yahoo dot com (Hosh) wrote on: 16-Aug-2003 12:52
The speedtest is interesting. But the seemingly fastest way contains a pitfall for beginners who
just use it because it is fast and fast is cool ;)
Walking through an array with next() will cut of the first entry, as this is the way next() works ;)
If you really need to do it this way, make sure your array contains an empty entry at the beginning.
Another way would be to use
<?php
while ($this = current($array) ){
do_something($this);
next($array);
}
?>
There is an impact on speed for sure but I did not test it. I would advise to stick with
conventional methods because current(),next() in while loops is too error prone for me.
----
Manual Page -- http://www.php.net/manual/en/control-structures.while.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50900
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50900&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50900&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50900&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50900&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50900&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50900&report=yes
Search -- http://master.php.net/manage/user-notes.php