Usage of phpfor loop statements-PHP Tutorial

Source: Internet
Author: User
The usage of phpfor statements is described in detail. A for loop is just a little more code and is added to it in a loop. The common task involved in a loop is to set the initial values of some counter variables. Check that the condition statement is that the for loop is just a little more code and is added to it in a loop. The common tasks involved in a loop are:

Set the initial values of some counter variables.
Check that the condition statement is correct.
Code Loop in execution.
At the end of each iteration, incremental data passes through the cyclic counter.
The for loop allows you to define a simple code line for these steps. It seems to have a strange form, so pay close attention to the syntax!

The syntax of the for loop is:

For (expr1; expr2; expr3)
Statement

The first expression (expr1) is unconditionally evaluated once before the loop starts.

Expr2 is evaluated before each cycle starts. If the value is TRUE, the loop continues and the nested loop statement is executed. If the value is FALSE, the loop is terminated.

Expr3 is evaluated (executed) after each loop ).

Each expression can be null or multiple expressions separated by commas. In expression expr2, all expressions separated by commas are calculated, but only the last result is obtained. If expr2 is null, it means an infinite loop (like C, PHP considers its value TRUE ). This may not be as useful as imagined, because it is often expected that the break statement is used to end the loop rather than the true value of the for expression.


View Simple Example 1

The code is as follows:


FOR ($ I = 0; $ I <= 2; $ I ++)
{
Print "value is now". $ I ."
";
}


Output value

Value is now 0
Value is now 1
Value is now 2

In the first loop, $ I = 0, which means the expression ($ I <= 2) is true. Therefore, when the print statement is executed, $ I is incremented by 1 to 1.

In the second loop, $ = 1, which means the expression ($ I <= 2) is true. Therefore, when the print statement is executed, $ I is incremented by 1 to 2.

In the third iteration, $ I = 2, which means the expression ($ I <= 2) is true. Therefore, when the print statement is executed, $ I increments to 1 3.

In the fourth iteration, $ I = 3, which means the expression ($ I <= 2) is false. Therefore, PHP does not execute loops or print statements.

Instance 2

The code is as follows:


$ Brush_price = 5;

Echo"







";Echo" ";Echo" ";For ($ counter = 10; $ counter <= 100; $ counter + = 10 ){Echo" ";}Echo"
QuantityPrice
";
Echo $ counter;
Echo"
";
Echo $ brush_price * $ counter;
Echo"
";


Output value

Quantity Price
10 50
20 100
30 150
40 200
50 250
60 300
70 350
80 400
90 450
100 500

The following examples show numbers 1 to 10:

The code is as follows:


/* Example 1 */

For ($ I = 1; $ I <= 10; $ I ++ ){
Echo $ I;
}

/* Example 2 */

For ($ I = 1; $ I ++ ){
If ($ I> 10 ){
Break;
}
Echo $ I;
}

/* Example 3 */

$ I = 1;
For (;;){
If ($ I> 10 ){
Break;
}
Echo $ I;
$ I ++;
}

/* Example 4 */

For ($ I = 1, $ j = 0; $ I <= 10; $ j + = $ I, print $ I, $ I ++ );
?>


Of course, the first example looks the most normal (or the fourth one), but you may find it convenient to use an empty expression in a for loop in many cases.

PHP also supports the replacement syntax of the for loop with colons.

The code is as follows:


For (expr1; expr2; expr3 ):
Statement;
...
Endfor;


We often need to traverse the following array:

The code is as follows:


/*
* We want to change the values of some elements in the following array during traversal.
*/
$ People = Array (
Array ('name' => 'Kalle', 'Salt' => 856412 ),
Array ('name' => 'Pierre ', 'Salt' => 215863)
);

For ($ I = 0; $ I <sizeof ($ people); ++ $ I)
{
$ People [$ I] ['Salt'] = rand (000000,999 999 );
}
?>


The problem with the above code is that the second expression of for will lead to slow code execution-because the length of the array needs to be calculated once every loop. Since the length of the array remains unchanged, we can use an intermediate variable to store the length of the array, and then use this variable as the second expression of the for loop. In this way, you can directly use the value of this variable during the loop without re-computing each time. As follows:

The code is as follows:


$ People = Array (
Array ('name' => 'Kalle', 'Salt' => 856412 ),
Array ('name' => 'Pierre ', 'Salt' => 215863)
);

For ($ I = 0, $ size = sizeof ($ people); $ I <$ size; ++ $ I)
{
$ People [$ I] ['Salt'] = rand (000000,999 999 );
}
?>

Bytes. The common task involved in a loop is to set the initial values of some counter variables. Check that the condition statement is...

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.