PHP basic flow control statements

WBOY
Release: 2016-07-25 08:59:17
Original
1222 people have browsed it
  1. a=11;
  2. if(a>10){
  3.   echo "a>10";
  4. }
Copy code

b, multi-branch: basic syntax:

if(conditional expression){

 Statement;

  //....;

}else{

 Statement;

  //....;

}

c, multiple branches: basic syntax:

if(conditional expression){ Statement; n statements; }else if(conditional expression){ Statement;n statements; }elseif(conditional expression){ Statement;n statements; }eles{ Statement;n statements; }

Tips: 1. Else if can have one or more. 2. The last else can be omitted

d, switch branch statement

switch(expression){ case constant 1:

  Statement; n statements;

  break;

 case constant 2:

  Statement; n statements;

  break;

 case constant 3:

  Statement; n statements;

  break;

 default:

  Statement; n statements;

  break; }

Note: 1. There are one to many case statements 2. The defaul statement does not need to be included (according to the business logic of your own code) 3. Usually, after the case statement, break is required to indicate exiting the switch statement. 4. Type of constant (int, float, string, Boolean)

Key point: The program is first configured in case order. If none is matched, the contents of the default statement will be executed until break is encountered, and then the switch will exit;

Comparison of if and switch branches: if judges a certain range, and switch judges a point, so we can select them like this: Application scenario: When the branches are just a few points (such as determining the direction of a tank), use switch. When the branch is a judgment of several areas (ranges), consider using if.



Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!