Home > Backend Development > PHP Tutorial > four leaf clover PHP loop statement notes foreach,list

four leaf clover PHP loop statement notes foreach,list

WBOY
Release: 2016-07-29 08:47:25
Original
891 people have browsed it

Generally foreach is used more

Copy the code The code is as follows:


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

echo "$changpin=>$jiage". '
';
}
?>

I really didn’t pay much attention to it before, but today I did it by myself, it’s not bad, I learned new things again, but I’m still too good at it, ehlist() 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 the 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 to see the effect, understand it, and write a post in case I forget to read it later. It is relatively superficial and the language is not well expressed, which is ridiculous.



Copy the code

The code is as follows:


/*
*Loop statement study notes in PHP

*1.while loop
if (expression)
Only execute one statement at a time.
while(expression){

Execute this loop body repeatedly; } *2.do-while loop*3.for loop*There are two types of loops depending on the loop conditions

*One: counting loop for

*Another type: conditional loop while do-while //foreach
*Several statements related to loops
*break;//can be used for process control and loop bodies to break 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 ' bgColor="'.$bg.'">';
}
echo '';
$i++;
if($i%10==0){
echo '';
}
}
echo '

Use a while loop to output the table

< ;/caption>';
$i=0;
while($i<1000){
if($i%10==0){
if($i%20==0){
$bg=" #ffffff";
}else{
$bg="#cccccc";
}
echo '
' .$i.'
';
//
$i=0;
do{
echo "$i :this is do*while
";
$i++;
}while($i<10);
//
for (Initialization condition; conditional expression; increment) {
Loop body;
}
/*
do-while loop is to execute the code once first and then judge, while
while loop is to judge first, and continue looping if it is true , 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 '
';



The above introduces the four leaf clover PHP loop statement notes foreach, list, including the content of four leaf clover. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template