note 60094 added to control-structures.foreach

From: Date: Mon, 26 Dec 2005 16:25:46 +0000
Subject: note 60094 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-100743@lists.php.net to get a copy of this message
In reply to FatalError, if you use Zend Dev Studio, i'll notice it's report "while(list() = each())" calls as "Using While w/ arrays param". So far, it's may be ambiguous to make things like that. Prefer to make a clean code and use PHP "kernel" functions to navigate through an array. Btw, if we take the exec time as example: //a litte array, just to test =-] $arr = array(); for($i=0; $i<100000; $i++) $arr[$i] = $i; //list-each solution $myTime = microtime(true); reset($arr); while (list($key, $value) = each($arr)) { $value = ($key * $value); } $myTime = microtime(true) - $myTime; echo("list-each : " . $myTime . "<BR />\n"); //foreach solution $myTime = microtime(true); foreach ($arr as $key => $value) { $value = ($key * $value); } $myTime = microtime(true) - $myTime; echo("foreach : " . $myTime . "<BR />\n"); /* output: list-each : 0.40198588371277 foreach : 0.23317384719849 */ ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60094 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60094&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60094&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60094&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60094&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60094&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60094&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60094&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60094&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100743) next »