This article will introduce you to the use of basic loop statements in PHP, including the use of for loops, while loops and do while loops. This article is very suitable for beginners of PHP.
Use the for statement to control multiple variables, thereby realizing advanced applications of multiple loops.
Let’s look at a program that uses a for statement to output a 9*9 multiplication table:
The code is as follows | Copy code | ||||
$sum=$i*$j; echo $sum ."t";
}
|
Basic structural form:
for(expression1;expression2;expression3){
Execution statement body
}The execution process is as follows:
1. Execute expression 1 first;
2. Then judge whether expression 2 is true or false. If it is false, jump out of the for loop and execute the next php statement. If it is true, enter the for loop to execute the statement body;
3. Then execute expression 3;
代码如下 | 复制代码 |
for($i=0;$i<=9;$i++){ |
4. Return to step 2 for loop operation;
Flow chart:
Example:
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||
for($i=0;$i<=9;$i++){
echo $sum ."t";
Statement body }while(expression)The do...while loop statement first executes the statement body once, and then judges the condition of the expression. If the value is true, it returns and loops again. If it is false, it jumps out of the loop.
The main difference between the two is that the first loop of the do...while statement must be executed. If the execution statements of the two loop bodies are the same, their running results are generally the same, but when the expression is false at the beginning, the running results of the two loops are different. Example:
Copy code
|
|
The function of the continue statement is to end this loop and enter the next loop, not to exit the entire loop program.
Example:
Previous article:A simple PHP file upload sample program_PHP tutorial
Next article:php if else swicth conditional control statement study notes_PHP tutorial
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|