note 44795 added to control-structures.foreach
| From: | pearcec at perfectorder dot com | Date: | Mon, 16 Aug 2004 20:16:37 +0000 |
| Subject: | note 44795 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74777@lists.php.net to get a copy of this message | ||
The example of while and foreach being indentical is not true. If you use foreach it doesn't
seem to use the internal array counter. Unlike the while loop which is calling each everytime. As
you can see in the example of the foreach we don't call next everytime. We only want it when
we get perl. Any there for does not advance the array counter as you think it will.
<?PHP
$lang =
array('Java','Perl','PHP','Python','Ruby');
foreach ($lang as $l) {
if (preg_match('/Perl/', $l)) {
echo next($lang); // echo Perl
}
}
while (list(, $l) = each($lang)) {
if (preg_match('/Perl/', $l)) {
echo next($lang); // echo Python
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44795
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44795&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44795&report=yes
Search -- http://master.php.net/manage/user-notes.php