PHP three major flow control statements comparison description

Source: Internet
Author: User
Tags case statement
PHP has three major process controls: Sequential control, Branch control, and loop control.

1, sequential control: is the program in order from the top down step-by-step execution.

2, Branch control: The program has a selective execution. It is divided into single branches, multiple branches, and multiple branches.

A, single branch: basic grammatical structure:

An if (conditional expression) {statement; //.....;}

Hint: No matter how complex the conditional expression is, it will eventually be true or false;

Example

A=11;if (a>10) {echo "a>10";}

B, multi-branch: Basic syntax:

An 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;} A eles{statement; n statements;}

Tip: 1, else if there can be one or more. 2, the last else can not

d, Switch branch statements

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;}

Attention:
1, Case statement has one to many
2, Defaul statements can not (according to their own code of business logic)
3, usually, after the case statement, to take a break, indicating the exit switch statement
4. Type of constant (int, float, string, Boolean)

Emphasis: The program is configured first in case order, and if none match, then the contents of the default statement are executed, and the switch is exited until a break is encountered;

If and switch branch comparison:
If the judgment of a range, and switch is the judgment of a point, so we can choose them:
Application scenario: When the branch is a few points (such as judging the direction of the tank), use Swtich.
When a branch is a judge of several areas (ranges), consider using IF.

Cycle Control Flow

For (cyclic initial value; loop condition; step) {//n multiple statements;}
while (loop condition) {//Loop body, statement}
do{//cyclic body}while (cyclic condition);

We should ensure that the $_request[' parameters ' are consistent with the names of the HTML elements given in the submission data page, according to normal thinking. If not, the notice prompt appears. At the same time we accept that the data is null, equivalent to "".

$_request the method can accept the user's post or GET request data

Break and Continue

Loop-related statement-break

Basic concept: Indicates the end of the current for, while, do. While, switch, process, you can give a number that indicates exiting to the first layer.

$i =0;while (+ + $i) {switch ($i) {case 5:echo quit At5;break;case 10:echo quitat 10;break 2;default:break;}} echo ' $i = '. $i;

The result is:

Quit at 5quit at 10$i=10

From the above case, we get a few conclusions:

1. Break statement jumps out of layer 1 by default

2. The number after the break statement cannot exceed the number of loop layers that can actually be jumped out, otherwise it will be reported fatalerror

Circular related Statements-continue

Basic concept: Continue is used to end this cycle of the remaining code, start a new cycle (if the condition is true, continue to execute), continue can also be followed by a number, indicating a restart from the first few cycles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.