PHP Branching and looping

Source: Internet
Author: User
Tags case statement

First, Overview:

in the previous chapter we explained The operators and expressions in PHP, through the above knowledge point, we can do some basic operations. But when it comes to some more complex logic, branching and looping are essential. The combination of branching and looping can make your business more complex and your code more powerful.

ii. Common branch structure if statements

1 single If statement

Basic Format:

if (conditional expression) {

Statement Group ;

You can omit "{}" when a statement group is a single statement.


When the value of the conditional expression is true (true), PHP executes the statement group, and when the value of the conditional expression is False (false), PHP does not execute the statement group, ignoring the statement group executing the following statement, which can be referenced by the

2 bidirectional conditional branching statements

The format is as follows

if (conditional expression) {

Statement Group 1


Statement Group 2


The If-else conditional judgment is similar to the IF condition, when the conditional expression value of the If-else statement is True (true), if the Ontology statement (statement Group 1) is executed, and if the conditional expression value is False (FALSE), the entity statement of else is executed (statement Group 2).

3 multi-direction conditional branching statements

The format is as follows

if (conditional expression 1) {

statement block 1

}elseif (conditional expression 1) {

statement block 2

}elseif (conditional expression N) {

statement block N


statement block n+1


similar to the above, just add multiple ElseIf options after the IF, so that the entire statement can filter more conditions.

iii. common branching structure switch statements

switch-case statement syntax:

switch (expression) {

Case value 1:

statement sequence 1; break;

Case Value 2:

statement sequence 2; break;


statement sequence n; break;


when the program executes a switch condition, it takes out the key value and compares it to the case. When a matching condition is encountered, the statement inside the case is executed until the break statement is encountered.

It is important to note that the switch statement differs from the IF statement in that it can only determine the identity relationship, and that the constant of the case clause in the switch statement can be an integer constant, a character constant, an expression, or a variable, and cannot be another type.

In the same switch, the constant of the case clause cannot be the same, otherwise the second value never matches.

iv. Common loop statements

1 while statement

While loop syntax:

while (expression) {

Statement or statement sequence


when the expression in the While loop statement is true, the program will always execute the contents of the loop body until it is satisfied that the condition is false to stop executing the program. If the condition is always true, then the loop will continue to loop, which is the dead loop. So when we use loops, we need to pay attention to the following two principles.

2 Do and statements

Basic Format:


Statement or statement sequence

}while (expression);

The program executes the statement in the body of the Do statement (the body of the loop), then checks the value of the expression (the loop control statement), and if it matches the condition (the value is true), then the statement in the DO statement body is executed until the condition does not meet the stop.

3 for Loop

Basic Format:

for (expression 1; expression 2; expression 3)


A sequence of statements or statements ;


In the For Loop statement, the expression 1 is the initial condition of the loop, the expression 2 is the loop control condition, the expression 3 increments the control variable, and the statement or statement sequence is the loop body .

4 Special Control statements

1. Break

We have used the break keyword before in the switch condition, which causes the program to terminate the statement in the current scope switch ontology, and if break is used in a for, while, or do-while loop structure, the program will jump out of the loop

2. Continue

Continue is a bit like break, continue if used in a for, while, or do-while loop structure, when the program executes to continue, the subsequent statement is skipped directly, and the next loop is executed directly

3. Exit

The current script simply executes to the exit statement, regardless of the structure in which it exits the current script directly.

Through this chapter, we can clearly understand the use of branches and loops, you can better control the process to deal with complex business, you can feel the code is more and more chaotic. The next section will learn about the use of functions and how to write more concise and efficient code.

PHP Branching and looping

Related Article

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: 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.