note 46540 added to control-structures.foreach

From: Date: Thu, 14 Oct 2004 20:31:44 +0000
Subject: note 46540 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-78554@lists.php.net to get a copy of this message
In response to dual iteration using a for instead of foreach (andy at barbigerous dot net, endofyourself at yahoo dot com): The while evaluation of your for loop will break only if the last expression in it returns false when using commas instead of &&/AND*. <?PHP // This will continue until list(,$val1) = each($arr1) is false for( ; false , list(,$val1) = each($arr1) ; ) // This will break immediately for( ; list(,$val1) = each($arr1) , false ; ) // Here is your original example for( $someArray1.reset(), $someArray2.reset(); list(, $someValue1 ) = each( $someArray1 ) , list(, $someValue2 ) = each( $someArray2 ) ; ) // This will produce expected results for( $someArray1.reset(), $someArray2.reset(); list(, $someValue1 ) = each( $someArray1 ) && list(, $someValue2 ) = each( $someArray2 ) ; ) ?> The one situation where you would want to use a comma is if you wanted to continue iterating until only your last condition returned false, but this circumstance is rare. * If using AND, be sure to enclose each condition in parenthesis since && will evaluate before the assigment operator (=) and AND after. ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46540 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46540&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46540&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46540&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46540&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46540&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46540&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46540&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78554) next »