Home  >  Article  >  Backend Development  >  Detailed explanation of flow control statements and loop control statements in PHP

Detailed explanation of flow control statements and loop control statements in PHP

黄舟
黄舟Original
2017-10-23 09:08:121782browse

1. There are four main types of flow control statements: if, ii...else, elseif (sometimes it can also be written as else if), and switch. The statement format in PHP is:

if (condition is met) {execution statement}

if (condition is met) {execution statement} else {execution statement}

if (condition Satisfy) {Execution statement} elseif {Execution statement} elseif {Execution statement} ..... else {Execution statement}

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

     case 2:Statement;break;

     case 3:      

##      default:       }

if:There is only one condition

If...else: There are two conditions

elseif: There are multiple conditions

Switch: There are multiple conditions When there are multiple conditions, the elseif and switch statements have the same effect. However, in order to avoid complicated and lengthy statements, use switch statements

2. There are three main types of loop control statements: while, for, and do while. For example, to output all integers less than 5, the statement format in PHP is:

 *******while语句*******  
  $i = 0;
  while($i<5)
  {
    echo $i;
    $i++;
  }
  *******for语句*******
  for($i = 0;$i < 5;$i++)
  {
    echo $i;
  }
  ******do while语句*******
  $i = 0;
  do
  {
    echo $i;
    $i++;
  }while($i<5);

[Note]

1. The while loop implementation does not know the number of loops, while the for loop knows the number of loops.

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

【Keyword】

Break: Terminate the loop

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

The above is the detailed content of Detailed explanation of flow control statements and loop control statements in PHP. 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