note 44795 deleted from control-structures.foreach by tomsommer

From: Date: Mon, 16 Aug 2004 20:24:19 +0000
Subject: note 44795 deleted from control-structures.foreach by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74779@lists.php.net to get a copy of this message
Note Submitter: pearcec at perfectorder dot com ---- 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 } } ?>

« previous php.notes (#74779) next »