PHP loop statement note (foreach, list) _ PHP Tutorial

Source: Internet
Author: User
PHP loop statement notes (foreach, list ). Generally, foreach uses the following code to copy more code :? Php $ pricearray (apple10, orange20, banner30); foreach ($ priceas $ key $ value) {echo $ key .. $ value.br;} echobr generally uses many foreach functions

The code is as follows:


$ Price = array ('apple' => 10, 'Orange '=> 20, 'banner' => 30 );
Foreach ($ price as $ key => $ value)
{
Echo $ key. '=>'. $ value .'
';
}
Echo'
';
?>


There is also a more advanced and common method

The code is as follows:


$ Shuiguo = array ('apple' => 10, 'Orange '=> 20, 'banner' => 30 );

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


I haven't paid much attention to it before. today I am doing it myself. it's good to know about new things, and I am still doing it myself.

The list () function can be used to break down an array into a series of values and name new variables. Click here if you do not understand list

The two pieces of code output are the same.

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.

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 ","
";
}
?>


In this way, you can still use the array $ price.

I have some in the book. as a newbie, I have to do it myself. I have to think about the effect, understand it, write a post, and forget to look at it later. it's superficial, and the language is poor.

The code is as follows:


/*
* Learning notes for loop statements in PHP
* 1. while loop
If (expression)
Execute only 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 based on different cyclic conditions.
* One Type: counting loop
* Another Type: conditional loop while do-while // foreach
* Several loop-related statements
* Break; // it can be used for process control and loop body to jump out of the loop.
Continue; // it can only be used for the loop body to exit this loop. Exit;
Return;
* The write cycle should not exceed three layers.
* The number of cyclic flow control statements should not exceed five layers.
*/
$ Num = 0;
While ($ num <100 ){
Echo "this is the result of executing the {$ num} output.
";
$ Num ++;
}
//
Echo'




















Align = "center"> ';Echo' ';$ 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'
Use a while loop to input
Output Table
'. $ I .'
';
//
$ I = 0;
Do {
Echo "$ I: this is do * while
";
$ I ++;
} While ($ I <10 );
//
For (initial condition; condition expression; increment ){
Loop body;
}
/*
The do-while loop is to execute the code first and then judge, and
The while loop is first judged. if it is true, the loop continues.
False is not a loop.
*/
// Multiplication table
For ($ I = 1; $ I <= 9; $ I ++ ){
For ($ j = 1; $ j <= $ I; $ j ++ ){
Echo "$ j x $ I =
". $ J * $ I ."";
}
Echo'
';


The pipeline code is as follows :? Php $ price = array ('apple' = 10, 'Orange '= 20, 'banner' = 30); foreach ($ price as $ key = $ value) {echo $ key. '= '. $ value. 'Br ';} echo 'br...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.