Home > Article > Backend Development > Notes on the basic syntax structure of PHP loop statements_PHP tutorial
There are many loop statements in PHP, such as: for, foreach, while, do while, goto statements. Let me briefly introduce the usage of these loop statement structures.
for loop control
for(initial value of loop; condition of loop; step size){
//Execute statement;
}
Example
The code is as follows | Copy code | ||||
{ print "value is now " . $i . "";
} |
value is now 0
value is now 1value is now 2
In the first loop, $i=0, which means that the expression, ($i<= 2), is true. Therefore, when the print statement is executed, $i is incremented by 1 and becomes 1.
In the second loop, $ = 1, which means that the expression, ($i<= 2), is true. Therefore, when the print statement is executed, $i is incremented by 1 and becomes 2.
In the third iteration, $i= 2, which means that the expression, ($i<= 2), is true. Therefore, when the print statement is executed, $i is incremented and becomes 1 3. In the fourth iteration, $i= 3, which means the expression, ($i<= 2), is false. Therefore, PHP does not execute the loop and does not execute the print statement.
while loop
The basic grammatical structure is
while(loop condition){
//The loop condition value changes. If it is not added, it will become an infinite loop
代码如下 | 复制代码 | ||||||||||||||||||||||||
"; $a++; }?> } Example
do..while loop control Basic grammatical structure do{ //Execute statement; //The loop condition value changes. If it is not added, it will become an infinite loop
Basic concept: Indicates the end of the current for, while, do..while, switch, process. You can give a number to indicate which layer to exit to. 1. The break statement will jump out to level 1 by default 2. The number after the break statement cannot exceed the actual number of loop levels that can be jumped out, otherwise, a fatal error will be reported Basic concept: continue is used to end the remaining code of this loop and start a new loop from scratch (if the condition is true, continue execution), continue can also be followed by a number to indicate the number of loops to restart from goto statement Basic concept: Through the goto statement, we can jump the program to a specified place for execution. goto tag; Tag: Statement; Quick Start Case:
5. The name of the constant, we generally say it is all capital letters, and then separated by underscores Statement: 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 Previous article:PHP+ajax implements a news message system without refreshing (with source code)_PHP tutorialNext article:PHP+ajax implements a news message system without refreshing (with source code)_PHP tutorial Related articlesSee more |