I am new to PHP. My question is when I use the following script:
$arr1 = array('fname' => 'niraj','lname' => 'kaushal','city' => 'lucknow');
while(list($key, $value) = each($arr1)){
echo "$key has $value value <br>";
}
foreach($arr1 as $key => $value){
echo "$key:$value <br>";
}
displays
fname has niraj value
lname has kaushal value
city has lucknow value
fname:niraj
lname:kaushal
city:lucknow
but when I change the order of foreach and while, as follows
$arr1 = array('fname' => 'niraj','lname' => 'kaushal','city' => 'lucknow');
foreach($arr1 as $key => $value){
echo "$key:$value <br>";
}
while(list($key, $value) = each($arr1)){
echo "$key has $value value <br>";
}
he gives the following conclusion
fname:niraj
lname:kaushal
city:lucknow
Why the second script does not display the output of the while loop. What is the reason for this.
source
share