Home  >  Article  >  Backend Development  >  PHP loop statement notes (foreach, list)_PHP tutorial

PHP loop statement notes (foreach, list)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:22:30967browse

Generally, foreach is used more often

Copy code The code is as follows:

$price=array( 'apple'=>10,'orange'=>20,'banner'=>30);
foreach($price as $key=>$value)
{
echo $ key.'=>'.$value.'
';
}
echo '
';
?>

Also A more advanced and common method
Copy the code The code is as follows:

$shuiguo=array ('apple'=>10,'orange'=>20,'banner'=>30);

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

I really didn’t pay much attention to it before , I did it by myself today, not bad, I learned something new, but I am still too good at it, hey

The list() function can be used to decompose an array into a series of values, allowing new variables to be named. If you don’t understand lists, click here

The output of the two pieces of code is the same.

It should be noted that when using the each() function, the array will record the current element. If you want to use the array twice in the same script. You need to use reset() to reset the current element to the beginning of the array.
Copy code The code is as follows:

$price=array('apple'=> 10,'orange'=>20,'banner'=>30);
foreach($price as $key=>$value)
{
echo $key.'=> '.$value.'
';
}
echo '
';
reset($price);
while(list($key,$value)= each($price))
{
echo "$key=>$value","
";
}
?>

This way you can still use the array $price.

There are some in the book. As a novice, I do it myself, type it out to see the effect, understand it, and write a post in case I forget to read it later. What I said is relatively superficial and my language expression is not good. It’s funny. .

Copy code The code is as follows:

/*
* in PHP Loop statement study notes
*1. while loop
if (expression)
only executes one statement at a time.
while(expression){
Repeatedly execute this loop body;
}
*2.do-while loop
*3.for loop
*There are two types depending on the loop conditions Three types of loops
* One: counting loop for
* Another: conditional loop while do-while //foreach
* Several statements related to loops
*break;/ / can be used for process control and loop bodies to jump out of loops.
continue;//Can only be used in the loop body to exit this loop. exit;
return;
* Try not to exceed three levels of writing loops.
*Try not to exceed five levels of loop flow control statements.
*/
$num=0;
while($num<100){
echo "This is the result of executing the {$num}th output
";
$num++;
}
//
echo 'align="center">';
echo 'Use a while loop to output the
table';
$i=0;
while($i<1000){
if($i%10==0){
if($i%20==0){
$bg="#ffffff";
}else{
$bg="# cccccc";
}
echo '
onmouseover="lrow(this)" onmouseout="drow
(this)" bgColor="'.$bg.'">' ;
}
echo '';
$i++;
if($i%10==0){
echo '';
}
}
echo '
'.$i.'
';
//
$i=0;
do{
echo "$i :this is do*while
";
$i++;
}while($i<10);
//
for (initialization condition; conditional expression Formula; increment) {
Loop body;
}
/*
do-while loop executes the code once and then judges, while
while loop judges first, if it is true Just continue to loop, if it is
false, it will not loop.
*/
//Nine-Nine Multiplication Table
for($i=1; $i<=9; $i++){
for($j=1; $j<=$i ; $j++){
echo "$j x $i =
".$j*$i." ";
}
echo '
';

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324723.htmlTechArticleGenerally, foreach is often used to copy the code. The code is as follows: ?php $price=array('apple'=10, 'orange'=20,'banner'=30); foreach($price as $key=$value) { echo $key.'='.$value.'br'; } echo 'br...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn