There are several ways to write php for loop

藏色散人
Release: 2023-03-13 20:04:02
Original
2894 people have browsed it

php for loop has two ways of writing, namely: 1. "for(expr1; expr2; expr3){...}" method; 2. "for(expr1; expr2; expr3){if( ...){}break;}..." method.

There are several ways to write php for loop

#php How many ways to write a for loop?

How to write and examples of PHP for loop

For loop is one of the most recent loop statements. No matter which language, there is this loop statement, too Loop methods commonly used in our work.

Grammar rules:

for (expr1; expr2; expr3){
要执行的代码
}
Copy after login

expr1: Indicates where the loop starts

expr2: The condition of the loop. 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: Evaluated (and executed) after each loop.

The writing is a bit obscure, let’s write the simplest for loop demo!

for Loop demo1:

<?php
for($n=1;$n<20;$n++){
    echo &#39;for循环语句执行第&#39;.$n."次<br>";
}
Copy after login

Execution result:

for循环语句执行第1次
for循环语句执行第2次
for循环语句执行第3次
for循环语句执行第4次
for循环语句执行第5次
for循环语句执行第6次
for循环语句执行第7次
for循环语句执行第8次
for循环语句执行第9次
for循环语句执行第10次
for循环语句执行第11次
for循环语句执行第12次
for循环语句执行第13次
for循环语句执行第14次
for循环语句执行第15次
for循环语句执行第16次
for循环语句执行第17次
for循环语句执行第18次
for循环语句执行第19次
Copy after login

It can be seen that when the condition of $n<20 is not satisfied, $n is not output.

For loop statement Demo2, use break to jump out of the for loop:

<?php
for($n=1;$n<20;$n++){
    if($n==10){
        break;
    }
    echo &#39;for循环语句执行第&#39;.$n."次<br>";
}
Copy after login

Output result:

for循环语句执行第1次
for循环语句执行第2次
for循环语句执行第3次
for循环语句执行第4次
for循环语句执行第5次
for循环语句执行第6次
for循环语句执行第7次
for循环语句执行第8次
for循环语句执行第9次
Copy after login

When n is equal to 10, jump out of the loop, Do not continue execution. If we just want to jump out of 10 and continue to execute the others, we can write like this:

<?php
for($n=1;$n<20;$n++){
    if($n==10){
        continue;
    }
    echo &#39;for循环语句执行第&#39;.$n."次<br>";
}
Copy after login

In this way, we just jump out of a loop, and the result is as follows:

for循环语句执行第1次
for循环语句执行第2次
for循环语句执行第3次
for循环语句执行第4次
for循环语句执行第5次
for循环语句执行第6次
for循环语句执行第7次
for循环语句执行第8次
for循环语句执行第9次
for循环语句执行第11次
for循环语句执行第12次
for循环语句执行第13次
for循环语句执行第14次
for循环语句执行第15次
for循环语句执行第16次
for循环语句执行第17次
for循环语句执行第18次
for循环语句执行第19次
Copy after login

Recommended learning: "PHP Video Tutorial

The above is the detailed content of There are several ways to write php for loop. For more information, please follow other related articles on the PHP Chinese website!

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