note 52733 deleted from control-structures.while by cmb

From: Date: Wed, 30 Sep 2020 11:32:07 +0000
Subject: note 52733 deleted from control-structures.while by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215487@lists.php.net to get a copy of this message
Note Submitter: stuart ---- A note to anyone nesting a while loop inside a while loop.... Consider the example below: $one = array("10", "20", "30", "40"); $two = array("a", "b", "c", "d"); $i=0; while($i < count($one)) { while($a = each($two)) { echo $a[1]." - ".$one[$i].", "; } $i++; } This will return the following: a - 10, b - 10, c - 10, d - 10 So in effect the main while loop is only doing one iteration... and not 4 as expected.... Now the example below works as expected.. $i=0; while($i < count($one)) { foreach($two as $a) { echo $a." - ".$one[$i]."\n"; } $i++; } by returning: a - 10, b - 10, c - 10, d - 10, a - 20, b - 20, c - 20, d - 20, a - 30, b - 30, c - 30, d - 30, a - 40, b - 40, c - 40, d - 40 So there is clearly a difference on how while statements work in comparison to other looping structures. I think it would be good to have an explaination of this strange behaviour.

« previous php.notes (#215487) next »