Several "***" usage of for loop statements in PHP

Source: Internet
Author: User
The for statement can be said to be the most basic statement in the loop control part of PHP (also multiple languages). The Fulfillment rules and basic usage of the for statement are not mentioned here, see the for statement section in the PHP Manual. In the PHP Manual, its syntax definition, such as the for statement, can be said to be the most basic statement in the loop control part of PHP (also multiple languages, the fulfillment rules and basic usage of for statements are not mentioned here. for more information, see the for statements section in the PHP Manual. The syntax of PHP is defined as follows:

For (expr1; expr2; expr3)
Statement


The following describes several useful variants of the for statement.

1. infinite loop

The first is the well-known infinite loop (also known as the "death cycle "). Since null is a valid syntax, we can leave the three expressions in the for statement empty, which will result in the continuous execution of the for nested statement.

<? Php
For (;;){
// Place the statement to be executed continuously
}
?>


Although some tasks will be applied to an infinite loop, most Program tasks, especially the scope of PHP, will add some conditions to terminate the loop when applying an infinite loop.

<? Php
For (;;){
// If it is 2199 AD, it will jump out of the loop
If (date ('Y') = '000000 '){
Break;
}
}
?>


2. apply an empty expression

Next we will talk about the application of the null syntax in the initialization statement expr1. The most common function of leaving expr1 blank is to complete more complex initialization.

<? Php
If (isset ($ I )){
Unset ($ I );
If (int) date ('') <2008 ){
$ I = 0;
} Else {
$ I = 1;
}
} Else {
$ I = 3;
}

For (; $ I <10; $ I ){
Echo $ I;
}
?>


In the same way, the iteration expression expr3 may also be left blank. you can also use this to write more complex iterations, such as calling different iterations based on different conditions.

The expr2 field in the for statement is the infinite loop mentioned above. of course, you can add more complex conditions to determine when to jump out of the loop, which is not repeated here.

3. multiple cycles

Using multiple loops to hold multiple variables is also a feature that is often neglected in the for statement. In the following example, a double loop is usually used in a general task, and a triple or above loop is of little significance.

<? Php
For ($ I = 0, $ j = 10; $ I <= 10; $ I, $ j --){
Echo '$ I $ j = 10 \ r \ n ';
}
?>


The above code will be output:

0 10 = 10
1 9 = 10
2 8 = 10
3 7 = 10
4 6 = 10
5 5 = 10
6 4 = 10
7 3 = 10
8 2 = 10
9 1 = 10
10 0 = 10


4. more complex expressions

If the three expressions of the for statement are more complex, they can be used to optimize the algorithm. You can even use a for statement without a loop body to complete some tasks. For example, accumulate or factorial calculation:

<? Php
// Calculates the cumulative result from 1 to 5, and returns the value to $ j.
For ($ I = 1, $ j = 0; $ I <= 5; $ j = $ I );
Echo $ j;

// Calculate the factorial result from 1 to 5. the bin value is $ j.
For ($ I = 1, $ j = 1; $ I <= 5; $ j * = $ I );
Echo $ j;

?>


With the help of the C language syntax, PHP will certainly have the characteristics of C. for example, a strong for loop statement is a typical example.

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.