Analysis of several usages of php for loop statement_PHP tutorial

WBOY
Release: 2016-07-13 17:50:02
Original
812 people have browsed it

The syntax of for loop is:

The code is as follows. Copy the code
for (expr1; expr2; expr3)
statement

Let’s talk about several useful variations of the for statement.

1. Infinite loop

This is also called an infinite loop, which goes on like this without a beginning or an end


for (;;) {
//Place statements that need to be executed continuously
}
?>


If an infinite loop is combined with if else, break can also break out of the loop

The code is as follows. Copy the code

for (;;) {
//If it is 2199 AD, jump out of the loop http://www.hzhuti.com/nokia/n93/
if (date('Y') == '2199') {
break;
}
}
?>


2. Use empty expressions

The next step is to talk about using null syntax in the initialization statement expr1. The most common function of leaving expr1 blank is to complete more complex initialization work.

The code is as follows. Copy the code

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, and you can also use this to write more complex iterations, such as calling different iterations according to different conditions.

Leaving the conditional statement expr2 in the for statement blank is the infinite loop mentioned above. Of course, you can also add some more complex conditions to determine when to jump out of the loop, which will not be repeated here.

3. Multiple loops

Using multiple loops to control multiple variables is also a feature that is often overlooked in the for statement. As in the example below, double loops are generally used in general tasks, and loops of three or more are generally of little significance.

for ($i = 0, $j = 10;$i <= 10;$i++, $j--) {
echo "$i + $j = 10rn";
}
?>


The above code will 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

What to do if we want to break out of the loop halfway, example

Look at the following example of nested multiple loops:

for($i = 1;$i <= 10; $i++ ){
for($j = 1;$j <= 10;$j++){
$m = $i * $i + $j * $j;
echo”$m n
”;
if($m < 90 || $m > 190) {
break 2;
}
}
}

break 2 jumps out of the double loop. You can try it out and remove 2. The result will be completely different. If no parameters are used, only this loop will be jumped out, and the first level loop will continue to execute.

Note:
break is used in the various loops and switch statements mentioned above. Its function is to jump out of the current grammatical structure and execute the following statements. The break statement can take a parameter n, indicating the number of levels to jump out of the loop. If you want to jump out of multiple loops, you can use n to indicate the number of levels to jump out of. If there is no parameter, the default is to jump out of the current loop.

4. More complex expressions

If the three expressions of the for statement are written more complexly, 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, calculating accumulation or factorial:

//Calculate the cumulative result of 1-5, bin value to $j
for ($i = 1,$j = 0; $i <= 5;$j += $i++);
echo $j;

//Calculate the factorial result of 1-5, bin value to $j
for ($i = 1,$j = 1; $i <= 5;$j *= $i++);
echo $j;

?>


If I want to execute to a place, automatically call up the current loop and execute it - example

for($i = 1;$i <= 100; $i++ ){
if($i % 3 == 0 || $i % 7 == 0){
continue;
}
}else{
echo”$i n
”;
}
}
?>

The function of the PHP code snippet is to output those natural numbers within 100 that are neither divisible by 7 nor divisible by 3. In the loop, first use the if conditional statement to determine those numbers that can be divisible, and then execute the continue; statement. Entered directly into the next cycle. The following output statement will not be executed.

Note:

continue is used in a loop structure to control the program to abandon the code after the continue statement of this loop and move to the next loop. continue itself does not jump out of the loop structure, it just gives up the loop this time. If continue is used in a non-loop structure (such as an if statement, a switch statement), the program will error.

Excerpted from php development

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478302.htmlTechArticleThe syntax of the for loop is: The code is as follows Copy the code for (expr1; expr2; expr3) statement Let’s talk about the for statement Several useful variations. 1. Infinite loop This is also called an infinite loop, which is not opened...
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