Explanation of flow control statement and loop control statement of based on php

  • 2021-08-10 07:15:11
  • OfStack

1. There are mainly four kinds of process control statements: if, ii... else, elseif (sometimes it can be written as else if) and switch.

The statement format in PHP is:

if (condition satisfied) {execute statement}

if (condition satisfied) {execute statement} else {execute statement}

if {Execute Statement} elseif {Execute Statement} elseif {Execute Statement}...... else {Execute Statement}

switch (condition) {case 1: statement; break;

case 2: Statement; break;

case 3: Statement; break;

default: Statement; break;}

if: There are only 1 condition

if... else: There are two conditions

elseif: There are multiple conditions

switch: Multiple conditions When there are multiple conditions, the elseif and switch statements act the same way. However, in order to avoid complicated and lengthy statements, use switch statements

2. There are three kinds of loop control statements: while, for and do while. For example, output all integers less than 5.

The statement format in PHP is:

********while statement ********


    $i = 0;
    while($i<5)
    {
          echo $i;
          $i++;
    }

********for statement ********


    for($i = 0;$i < 5;$i++)
    {
          echo $i;
    }

*******do while Statement*******


    $i = 0;
    do
    {
          echo $i;
          $i++;
    }while($i<5);

[Attention]

1. The while cycle does not know the number of cycles, while the for cycle knows the number of cycles.  

2. In a complex PHP code, there may be multiple conditional control statements, loop control statements and functions, so it is very troublesome to find matching braces "{}". For this reason, PHP provides another writing format, including if, while, for, foreach and switch. The basic form of writing this form is to use colon ":" instead of curly braces "{" on the left, and use endif;, endwhile;, endfor;, endforeach;, endswitch; instead of curly braces "}" on the right.

"Keyword"

break: End Loop

continue: Terminate this loop and continue the next 1 loop until the end of the loop


Related articles: