Home  >  Article  >  Backend Development  >  Take you to understand the php loop structure

Take you to understand the php loop structure

伊谢尔伦
伊谢尔伦Original
2017-06-22 11:31:032213browse

One of the best functions of computer programs is to repeatedly perform certain operations according to specified conditions. The loop structure can reduce the workload of repeated writing of the source program, that is, when a given condition is true, a certain program segment is repeatedly executed until the condition is not true. The given conditions are called loop conditions, and the repeatedly executed program segment is called the loop body. In PHP, while loop, do-whileloop loop and for loopThree types.

Loop: Do one thing over and over again within limited conditions.
php for while do...while
1.for
Format
for (starting condition, termination condition, step size) {
Loop body;
}
Example:
300 Dazhong Temple--》Dazhong Temple
Start and end event interval
5:30 7e6edf38cd750d35c18caaf250ef4ee2";
}
Loop nesting
9*9
2.while
Format
Starting condition
while (termination condition) {
Loop body;
Step value;
}
Note: If you do not write the step value, the loop will become an infinite loop
The execution process of while and for is the same
3.do...while()
Format
Start condition
do{
Loop body;
Step value;
}while();
Note: do...while will be executed once regardless of whether the termination condition is true or not
4.break continue
break terminates the loop
continue ends this loop and continues the next loop
for while do...while effectively changes the loop state

while loop

whileThe loop needs to set a Boolean condition. When the condition is true, it continuously executes a statement block until the condition is false. After the program executes the while statement, it will perform the following operations:

  • Calculate the value of the expression and determine it is TRUEstillFALSE.

  • If the expression is FALSE, the while statement will end, and then the statements following the while statement will be executed.

  • If the expression is TRUE, execute the code block in the curly braces of the while statement, and then return to step 1 for execution.

Demo

<html>
    <head><title>使用while循环嵌套输出表格</title></head>
    <body>
        <table style="align: center;width:600px" border="1">
            <?php                $out = 0;                                   
                while( $out < 10 ) {                        
                    $bgcolor = $out%2 == 0 ? "#FFFFFF" : "#DDDDDD";
    
                    echo "<tr style=&#39;background-color:".$bgcolor."&#39;>"; //指定行的背景颜色
                
                    $in = 0;                                
                    while( $in < 10 ) {                     
                        echo "<td>".($out*10+$in)."</td>";  //执行一次,输出一个单元格
                        $in++;                              
                    }                    echo "</tr>";                           
                    $out++;                                 
                }            ?>
        </table>
    </body>
</html>

do...while loop

do...whileandwhileLoops are very similar, except that the value of the expression is checked at the end of each loop. The loop statement of do...while must be executed once, because the result value of the expression is checked after each loop.

Demo

<?php 
  $count = 0;
  do {    
      echo $count;
      $count++;
  }
  while ($count < 10);
?>

for loop

<span style="text-decoration: none;">for</span>Loop statements apply In the case where the number of repeated executions is clearly known, that is, the for statement predefines the variable for the number of loops in the for statement. The for statement is separated by semicolons into three parts, namely loop variable initialization, conditional expression and loop variable auto-increment or auto-decrement. Initialization is an assignment statement, which is used to assign an initial value to the loop control variable; the conditional expression is a relational expression, which determines when to exit the loop; increment defines the loop control variable, and how it changes after each loop. . When the program executes the for statement, the following operations will be performed:

  • When entering the for loop for the first time, initialize the loop control variable. value.

  • Decide whether to continue executing the loop based on the result of the judgment condition. If the judgment condition is true, continue executing the loop; if the condition is false, end the loop and execute the following statement.

  • After executing the statements in the loop body, the system will change the value of the loop control variable according to the increase or decrease of the loop control variable, and then return to step 2 to re-judge whether to continue executing the loop.

Demo

<?php 
    for( $i = 1;  $i <= 10;  $i++ )            
        echo "这是第<b> $i </b>次循环执行输出的结果<br>";    
?>

The above is the detailed content of Take you to understand the php loop structure. For more information, please follow other related articles on the PHP Chinese website!

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