PHP Basic Tutorial Four Process Control

Source: Internet
Author: User
Tags case statement

What this section explains

    • Sequential Process Control

    • Single Branch

    • Dual Branch

    • Multi-Branch

    • Switch

    • For

    • While

    • Do...while

    • Break

    • Continue

    • Goto

Objective

PHP's Process control and other languages are very similar, are divided into a lot of situations, we understand the code of the process, the general understanding of the code, the idea of a preliminary understand the role of the code and what will happen. The process of PHP is broadly divided into two types, sequential processes, branching processes, and cyclic processes.

Sequential Process Control

The sequential process is that the parser will follow the PHP code, one line of the parsing;

If there are no process control statements in our PHP code, then our PHP code executes sequentially. .

$a = n; $b =; $c =; $res = $a + $b * $c; Echo $res;

Like the code above, the parser will parse the line and execute it.

Branching Process Control

The branching process is the code that executes when something happens to our code. For example, in some cases, the common branches in PHP are:

    • Single Branch

    • Dual Branch

    • Multi-Branch

Single Branch

Execute specific code when our code conforms to a situation
The language format for a single branch is:

    if (conditional expression) {        //code block    }

Example:

<?php$a = 13;if ($a >) {//$a meet conditions greater than 12 to execute the following sentence    echo ' $a value greater than 12<br> ';} Echo ' Here is a single branch outside '; Result ... the value of $a is greater than 12 here is a single branch outside

Flow chart

The IF statement in a single branch is followed by curly braces. When the code inside the branch is executed, the parser will then parse the following code

Dual Branch

Do you think that when the above variable $ A does not meet the conditions, do not do anything, most of it is not so, when the expression of if bracket is not satisfied, we generally have the processing scheme, which is the dual branch structure, the language format

    if (conditional expression) {        //True code    }else{        //is code executed when False    }

Example:

$a = 6;if ($a >) {    echo ' $a value is greater than 12<br> ';} else{    echo ' $a value less than}echo ' here is a single branch outside '; Result ... the value of $a is less than 12 here is a single branch outside

Flow chart:

In the above code, when $ A does not satisfy more than 12 o'clock, the code inside the else is executed before the outside code is executed. If...else appear in pairs. But there can be no else.

When more than one if...else is present in the code, there is no logical relationship between them, and after executing a set of if, it executes another set of IF.

Multi-Branch

Sometimes when our two-branch execution is complete, there are branches that need to be judged, always judged, and then used in multiple branches. In multiple branches it is not necessarily necessary to have else,else if there can be more than one.

The basic syntax is:

if (conditional expression) {    statement}else if (conditional expression) {    statement}else if (conditional expression) {    statement} ...

Example:

<?php$a = 3;if ($a >) {    echo ' $a greater than 10 ';} else if ($a > 2) {    echo ' $a is less than or equal to 10 and greater than 2 ';} else{    Echo ' $a less than or equal to 2 ';} ..... Result ... $a less than or equal to 10 and greater than 2

Flow chart:

As you can see in the flowchart above, when the $a is not greater than 10 , it is important to determine whether it is greater than 2, and if there is a subsequent execution, it continues until the condition is met.

Switch

In the above if...else, if the conditional expression is generally expressed in the range (can also be expressed as a specific value such as $ A = = 2), but when we use more than the specific worth of time, the above code will look very redundant, it is necessary to use the Switch,switch can also be said if ... Another way of writing about else.

A switch statement is similar to a series of if statements that have the same expression. In many cases it is necessary to compare the same variable (or expression) with many different values and execute different code depending on which value it equals. This is the purpose of the switch statement

The basic syntax for switch is:

switch (variable/expression/value) {case    variable/expression/value:        processing statement; break    ;    Case variable/Expression/value:        processing statement; break    ;    Case variable/Expression/value:        processing statement; break    ;    Default:        The above situation does not satisfy the execution of the statement;    break;}

Example:

<?php$a = ' B '; switch ($a) {case    ' a ':        the value of echo ' $a is a ';        break;    Case ' B ':        The value of echo ' $a is B ';        break;    Case ' C ':        The value of echo ' $a is C ';        break;    Default:        echo ' $a value does not conform to all of the above conditions ';        break;} ..... The result ... the value of $a is B

Flow chart:

The switch statement thought and if very much like, in the switch parenthesis value is we need to judge the value, we want to judge the value and the value after the case is the same, the same word executes the sentence under the box, when not the same time, executes the next case statement. Note that the case statement is followed by a colon:when all of the case executes, there is no value that matches the condition, and the statement inside the default is executed.

In the above we can see that there is a break under each case statement, which is a keyword that automatically jumps out of switch when the parser encounters a break, so that the case behind it will not be executed. (Do not go down if you meet the same).

Switch needs to be aware of:

    • When a value in a case statement matches the value of a switch expression, PHP starts executing the statement.

    • When the case statement is executed, it exits when a break is encountered, but if there is no break, it runs until the end of break or switch execution is met.

    • The statement inside the Defaule is that it matches anything that does not match any other case.

    • The type following the case can make integral type, float, String, Boolean, array,null, usually we use integers or strings.

Cycle Flow Control

In development we sometimes have this demand, we need to print a sentence 100 times, this time with a simple sequence of process control will be very troublesome, but also can do, but if it is 10,000 times ... At this point we need to think of another way of thinking, we can use the loop, loop output a word, and control the number of cycles, so we will be able to output the results we want.

For loop structure

The For loop is the most common and most commonly used loop in our development, which is the preferred structure when we know the number of loops , like 10,000 times above, we already know the need to loop 10,000 times. The main thing to learn for a For loop is to understand the execution flow of the For loop. By the execution process for the for, we can clearly know where the For loop is exiting and where it needs to be in the loop.

Syntax structure:

for (loop initial value, loop initial value ...; loop condition; increment, increment) {        statement loop body;}

Example:

for ($i = 0; $i < $i + +) {    echo ' This is for Loop <br> ';} .... Results .... echo This is for loop (10 times);

Flow chart:

In the flowchart above we can see the order in which the For loop is executed, and the for loop only jumps out of the loop when it is false.

Order of execution for the For loop:

    1. Executes the variable initialization of $ A = 0, and the sentence is executed only once throughout the For loop.

    2. Determines whether the value of $ A is less than 10, if less than 10, executes the statement inside the for loop, or exits the loop if not less than.

    3. After executing the statement inside the for loop, execute the increment expression, $a + + (see the increment, decrement operator in front),

    4. When $ A increases by 1 before the condition is judged, whether the $a is less than 10, if less than 10, executes the statement inside the for loop, or exits the loop if not less than.

    5. When $ A is not less than 10, jump out of the loop and execute the statement following the for loop.

It is important to understand the order in which the for loops are executed, and you can look at the case and draw a picture yourself.

For loop printing 99 multiplication table

The For loop can be nested, and when nested, the outside loop executes once, and the for loop inside executes before it executes.

<?phpfor ($i = 1; $i <= 9; $i + +) {//control the number of layers of the multiplication table, the first layer is 1, the second layer is 2, until 9; for    ($j = 1; $j <= $i; $j + +) {//The number of each layer, the first layer is 1 * 1 = 1; The maximum of each layer cannot be greater than the number of layers, like the first layer, cannot appear 1 * 2 = 2;        echo $i. '*' . $j. '=' . $i * $j. "  ;//Display    }    echo ' <br> ';//After a layer is displayed, remember to change the line. }

Results:

The 99 multiplication table, which is executed in accordance with the for loop process, but the for loop's judging condition needs to be determined, that is, the number of cycles for the For loop.

While loop structure

When the number of cycles is uncertain, the for loop is a bit weak, and we can choose the while loop. While loop when there is no specific condition, is a dead loop, that is, the parser will always be executed, never stop, so we use while loop, notice while loop out of the condition of the loop.

The syntax structure of the while loop:

while (loop condition) {    loop body statement;}

Example:

<?php$a = 12;while ($a <) {    echo ' $a value is '. $a. ' <br> ';    $a + +;} .... The result .... The value of the echo $a is 12 ....

Flow chart:

The above code, the initial value of $ A is outside the while, we may not know how many times we need to loop, but we know that when the value of $ A is less than 20, it keeps looping, but we can't let the value of $ A stay the same, if the value of $ A is not changed, the while loop is a dead loop. So there is a $ A growth statement in the loop body.

Do...while Cycle Control

Do...while and while loops are roughly the same, but with a little difference, the loop body of the Do...while loop is written in do, and the condition is written in the while, and the do...while, whether or not it satisfies the conditions in the while, executes the loop body inside the do.

Do...while grammatical structure: do{    loop body statement;}while (judging condition);

Example:

<?php$a = 12;do{    echo ' $a value is '. $a. ' <br> ';    $a + +;} while ($a < 20);

Flow chart:

From the flowchart, we can see that the loop body is executed first and the judgment is made.
Jumps out of the loop when the condition is not met.

Keywords used in Process Control

Break

The effect of break is that when a situation is met, you do not want to cycle, jump out of the current loop, that is, to end the current cycle, whether you have not satisfied the initial conditions, forced exit.
Break can end the execution of a for, while,do-while, or switch structure, and break can accept an optional numeric parameter to decide to jump out of a few loops (for multiple loops).

Example:

<?phpfor ($i = 0; $i < $i + +) {    if ($i = = 5) {break        ;    }    The value of Echo ' $i is '. $i. ' <br> ';}

Results:

Flow chart:

As can be seen from the results, when the value of $i equals 5, break, jump out of the loop, so the output statement output 5 sentences.

Continue

The Continue is used in the loop structure to skip this cycle and then to determine if the condition is met. Note: You are jumping out of a single loop, and break is jumping out of the entire loop structure.
Continue can accept an optional numeric parameter to decide to skip a few loops to the end of the loop

Example:

<?phpfor ($i = 0; $i < $i + +) {    if ($i = = 5) {        continue;    }    The value of Echo ' $i is '. $i. ' <br> ';}

Results:

Flow chart:

In the flowchart can be seen when the $i = = 5, when the continue jump out of this cycle, then the output below can not output, so see in the results there is no output $i = = 5 this situation.

Goto

The goto operator can be used to jump to another location in the program. The target location can be tagged with the target name plus a colon.

Example:

<?php$a = 12;goto A;echo ' This is the first position '; A:echo ' This is a second position '; Results...... This is a second position.

You can see that one of the output statements has no output, because Goto jumps to the a position, note that a is followed by a colon:
Also Goto can be used in loops, so you can also jump out of the loop.
Precautions for use:

    1. Usage precautions: Goto in PHP has a certain limit, only in the same file and scope
      , which means that you cannot jump out of a function or class method, or you can skip into another function.

    2. A common use is to jump out of a loop or switch, instead of a multi-layered break

Summarize

Process Control, in the development of the inevitable, we write code always in the process control, understand that the PHP Process Control also understand the overall framework of the code. At the same time for the For loop while the loop itself can be deepened, no matter what programming language, only their own constantly tapping the code to understand the truth. So you have to hit the code more and practise more.

The above is the PHP basic tutorial four Process control content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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