PHP loop statement notes of foreach list

  • 2020-05-10 17:47:59
  • OfStack

1. foreach is used more often
 
<?php 
$price=array('apple'=>10,'orange'=>20,'banner'=>30); 
foreach($price as $key=>$value) 
{ 
echo $key.'=>'.$value.'<br>'; 
} 
echo '<br>'; 
?> 

There is a more advanced and common approach
 
<?php 
$shuiguo=array('apple'=>10,'orange'=>20,'banner'=>30); 

while(list($changpin,$jiage)=each($shuiguo)) 
{ 
echo "$changpin=>$jiage".'<br>'; 
} 
?> 

Before also really did not how to pay attention to next, today oneself start work, return pretty good, knew new thing again, still oneself too dish, ah

The list() function can be used to split an array into series 1 values, allowing you to name new variables. Click here if you don't understand list

The output of the two pieces of code is one.

Note that when using the each() function, the array records the current element. If you want to use the array twice in the same script. You need to use reset() to drop the current element and reset it to the beginning of the array.
 
<?php 
$price=array('apple'=>10,'orange'=>20,'banner'=>30); 
foreach($price as $key=>$value) 
{ 
echo $key.'=>'.$value.'<br>'; 
} 
echo '<br>'; 
reset($price); 
while(list($key,$value)=each($price)) 
{ 
echo "$key=>$value","<br>"; 
} 
?> 

You can still use the array $price.

Some books, as a novice, I started my own work, knock to see the effect, understand, write a post, convenient later forget to see, said more superficial, language expression is not good, laughed.

 
<?php 
/* 
*PHP In the loop statement study notes  
*1.while cycle  
if( expression ) 
 Only perform 1 time 1 Statement.  
while( expression ){ 
 Execute the loop body repeatedly;  
} 
*2.do-while cycle  
*3.for cycle  
* There are two types of loops, depending on the cycling conditions  
*1 Species: counting cycles  for 
* In addition 1 Species: conditional cycle  while do-while //foreach 
* Several statements related to loops  
*break;// Can be used for process control and loop body, out of the loop.  
continue;// Can only be used in the body of the loop, exit the loop.  exit; 
return; 
* Try not to overwrite the write loop 3 Layer.  
* The flow control statement of the loop should not exceed 5 Layer.  
*/ 
$num=0; 
while($num<100){ 
echo " This is execution number one  {$num}  Secondary output <br>"; 
$num++; 
} 
// 
echo '<table border="1" width="800" 
align="center">'; 
echo '<caption><h1> use 1 a while Cycle to lose  
 The form </h1></caption>'; 
$i=0; 
while($i<1000){ 
if($i%10==0){ 
if($i%20==0){ 
$bg="#ffffff"; 
}else{ 
$bg="#cccccc"; 
} 
echo '<tr 
onmouseover="lrow(this)" onmouseout="drow 
(this)" bgColor="'.$bg.'">'; 
} 
echo '<td>'.$i.'</td>'; 
$i++; 
if($i%10==0){ 
echo '</tr>'; 
} 
} 
echo '</table>'; 
// 
$i=0; 
do{ 
echo "$i :this is do*while <br>"; 
$i++; 
}while($i<10); 
// 
for( Initialization conditions ; Conditional expression ; The incremental ){ 
 The loop body.  
} 
/* 
do-while A loop is just executing first 1 Sub-code and then judge while  
while The cycle is to judge first, if is true So I'm going to keep doing the loop  
false It doesn't loop.  
*/ 
//99 Multiplication tables  
for($i=1; $i<=9; $i++){ 
for($j=1; $j<=$i; $j++){ 
echo "$j x $i = 
".$j*$i."  "; 
} 
echo '<br>'; 


Related articles: