Home > Backend Development > PHP Tutorial > PHP Basic Tutorial 4 - Process Control

PHP Basic Tutorial 4 - Process Control

黄舟
Release: 2023-03-06 08:46:02
Original
1412 people have browsed it

Content explained in this section

  • Sequential process control

  • Single branch

  • Double branch

  • Multiple branch

  • switch

  • for

  • while

  • do…while

  • break

  • continue

  • goto

Preface

PHP process control and Other languages ​​are very similar and are divided into many situations. Once we understand the flow of the code, we can roughly understand the idea of ​​the code, and initially understand the function of the code and what results will occur. PHP processes are roughly divided into two types, sequential processes, branch processes, and cyclic processes.

Sequential process control

Sequential process simply means that the parser will follow the PHP code and parse it line by line;

If our There are no flow control statements in the PHP code, so our PHP code will be executed sequentially. .

$a = 12;
$b = 13;
$c = 15;
$res = $a + $b * $c;
echo $res;
Copy after login

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

Branch process control

The branch process is the code that will only be executed when a certain situation occurs in our code. For example, if it meets a certain situation, the common branches in php are:

  • Single branch

  • Double branch

  • Multiple branches

Single branch

When our code matches a certain situation, execute specific code
Single branch The language format of the branch is:

    if(条件表达式){
        //代码块
    }
Copy after login

Example:

<?php

$a = 13;
if($a > 12){//$a符合大于12的条件才会执行下面的这句话

    echo &#39;$a的值大于12<br>&#39;;
}

echo &#39;这里是单分支外边&#39;;
......结果.....
$a的值大于12
这里是单分支外边
Copy after login

Flowchart
PHP Basic Tutorial 4 - Process Control

The if statement of a single branch is followed by curly brackets of. When the code in the branch is executed, the parser will continue to parse the following code

Double branch

Have you ever thought about what happens when the above variable $a does not meet the conditions? When, don't we do nothing? Most of them are not like this. When the expression in if brackets is not satisfied, we usually have a solution. This is the double branch structure, language format

    if(条件表达式){
        //为真时执行的代码
    }else{
        //为假时执行的代码
    }
Copy after login

Example:

$a = 6;
if($a > 12){

    echo &#39;$a的值大于12<br>&#39;;
}else{
    echo &#39;$a的值小于12&#39;
}

echo &#39;这里是单分支外边&#39;;
......结果.....
$a的值小于12
这里是单分支外边
Copy after login

Flow chart:
PHP Basic Tutorial 4 - Process Control

In the above code, when $a is not greater than 12, the code inside else will be executed, and then the outside code will be executed. code. if...else appears in pairs. But there can be no else.

When multiple if...else appear in the code, there is no logical relationship between them. After one set of ifs is executed, another set of ifs will be executed.

Multiple branches

Sometimes after our double branches are executed, there are still branches that need to be judged. If we keep judging, we use multiple branches. branch. It is not necessary to have else in multiple branches. There can be multiple else if here.

The basic syntax is:

if(条件表达式){
    语句
}else if(条件表达式){
    语句
}else if(条件表达式){
    语句
}...
Copy after login

Example:

<?php
$a = 3;
if($a > 10){
    echo &#39;$a 大于10&#39;;
}else if($a > 2){
    echo &#39;$a 小于等于10并且大于2&#39;;
}else{
    echo &#39;$a小于等于2&#39;;
}
.....结果......
$a小于等于10并且大于2
Copy after login

Flow chart:
PHP Basic Tutorial 4 - Process Control

As you can see in the above flow chart, when $a is not greater than 10, it will execute downward . This is very important. Then judge Is it greater than 2? If there is more, continue execution until the conditions are met.

switch

In the above if...else, the conditional expression in if generally represents a range (it can also represent a specific value such as $a == 2) , but when we use multiple specific values, the above code will look very redundant. This requires the use of switch. Switch can also be said to be another way of writing if...else.

A switch statement is like a series of if statements with the same expression. There are many situations where you need to compare the same variable (or expression) with many different values ​​and execute different code depending on which value it equals. This is exactly what the switch statement is for

The basic syntax of switch is:

switch(变量/表达式/值){
    case 变量/表达式/值:
        处理语句;
    break;
    case 变量/表达式/值:
        处理语句;
    break;
    case 变量/表达式/值:
        处理语句;
    break;
    default:
        上面的情况都不满足执行的语句;
    break;
}
Copy after login

Example:

<?php

$a = &#39;b&#39;;

switch ($a) {
    case &#39;a&#39;:
        echo &#39;$a的值是a&#39;;
        break;
    case &#39;b&#39;:
        echo &#39;$a的值是b&#39;;
        break;
    case &#39;c&#39;:
        echo &#39;$a的值是c&#39;;
        break;
    default:
        echo &#39;$a的值不符合上面的全部情况&#39;;
        break;
}
.....结果.......

$a的值是b
Copy after login

Flow chart:
PHP Basic Tutorial 4 - Process Control

The idea of ​​the switch statement is very similar to if. The value in the switch bracket is the value we need to judge. Whether the value we want to judge is the same as the value behind the case. If they are the same, execute the statement below the case. When they are different, execute Next case statement. Note that there is a colon after the case statement: ; When all cases are executed and there is no value that meets the conditions, the statement in default will be executed.

We can see above that there is a break under each case statement. This is a keyword. When the parser encounters break, it will automatically jump out of the switch, so that the following cases will be Will not be executed. (When encountering the same one, it will not be executed downwards).

Things to note about switch:

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

  • 当执行case语句后,遇见break就退出,但是如果没有break,则一直运行下去,直到遇见break或switch执行结束。

  • defaule里面的语句是它匹配了任何和其他case都不匹配的情况。

  • case后面的类型可以使整型,浮点型,字符串,布尔值,array,null,通常情况下,我们一般用整数 或字符串。

循环流程控制

在开发中我们有时会有这样的需求,我们需要打印一句话100次,这时候用单纯的顺序流程控制就会很麻烦,但是也能做,但如果是一万次呢…这时候就需要我们想另一种解决思路,我们能不能利用循环,循环的输出一句话,并且控制循环次数,这样我们就会很顺利的输出我们想要的结果。

for循环结构

for循环在我们的开发中是最常见和最常用到的循环,这种循环是在我们知道循环次数的时候,首选的结构,像上面的一万次,我们已经知道了需要循环一万次。学习for循环最主要的是看懂for循环的执行流程。按着for的执行流程,我们可以清晰的知道for循环是在哪里退出,在哪里需要在循环。

语法结构:

for(循环初始值,循环初始值...;循环条件;增量,增量){
        语句循环体;
}
Copy after login

示例:

for($i = 0; $i < 10; $i++){
    echo &#39;这是for循环<br>&#39;;
}
....结果....
echo 这是for循环(十次);
Copy after login

流程图:
PHP Basic Tutorial 4 - Process Control

在上面的流程图中我们可以看到for循环的执行顺序,for循环只有在为假的时候跳出循环。

for循环的执行顺序:

  1. 先执行变量初始化$a = 0;在整个for循环中,这句话只执行一次。

  2. 判断$a的值是否小于10,如果小于10,执行for循环里面的语句,如果不小于则退出循环。

  3. 当执行完for循环里面的语句,执行增量表达式,$a++(看前面的递增、递减运算符),

  4. 当$a增加1之后再进行条件判断,$a是否小于10,如果小于10,执行for循环里面的语句,如果不小于则退出循环。

  5. 当$a不小于10的时候,直接跳出循环,执行for循环后面的语句。

明白for循环的执行顺序非常重要,可以看着案列,自己动手画一画。

for循环打印99乘法表

for循环是可以嵌套使用的,当进行嵌套的时候,外面的循环执行一次,里面的for循环执行完才执行外面。

<?php

for($i = 1; $i <= 9; $i++){ //控制乘法表的层数,第一层是1,第二层是2,一直到9;
    for($j = 1; $j <= $i; $j++){ //每一层的个数,第一层是1 * 1 = 1;每一层的最大的不能大于层数,像第一层,不能出现1 * 2 = 2;
        echo $i . &#39;*&#39; . $j . &#39;=&#39; . $i * $j . &#39;  &#39;; //进行显示
    }
    echo &#39;<br>&#39;; //一层显示完后,记着换行。
}
Copy after login

结果:
PHP Basic Tutorial 4 - Process Control

99乘法表,按照for循环的流程一步步执行,但是for循环的判断条件需要找准,也就是for循环的循环次数。

while循环结构

当我们的循环次数不能确定的时候,这时候for循环就显得有些无力,这时候我们可以选择while循环。while循环当没有特定条件的时候,就是一个死循环,也就是解析器会一直执行,永远不会停止,所以我们在用while循环的时候,注意while循环跳出循环的条件。

while循环的语法结构:

while(循环条件){
    循环体语句;
}
Copy after login

示例:

<?php

$a = 12;
while($a < 20){
    echo &#39;$a的值是&#39; . $a . &#39;<br>&#39;;
    $a++;
}
....结果....
echo $a的值是12....
Copy after login

流程图:
PHP Basic Tutorial 4 - Process Control

上面的代码,初始值$a是在while的外面,我们可能不知道需要循环多少次,但是我们知道当$a的值小于20就一直循环,但是我们不能让$a的值不变,如果$a的值不变,while循环就是一个死循环。所以在循环体里面有一个$a的增长语句。

do…while循环控制

do…while和while循环大致一样,但是有一点不同,do…while循环的循环体是在do里面写着,判断条件是在while里面写着,并且do…while不管是否满足while里面的条件,都会执行一次do里面的循环体。

do...while语法结构:

do{
    循环体语句;
}while(判断条件);
Copy after login

示例:

<?php

$a = 12;
do{
    echo &#39;$a的值是&#39; . $a . &#39;<br>&#39;;
    $a++;
}while($a < 20);
Copy after login

流程图:
PHP Basic Tutorial 4 - Process Control

从流程图中可以看到是先执行循环体,在进行判断。
当不满足条件后跳出循环。

流程控制中用到的关键字

break

break的作用是当满足某种情况时,不想再循环了,跳出当前循环,也就是结束掉当前的循环,不管你有没有满足最初条件,强制退出。
break 可以结束 for, while,do-while 或者 switch 结构的执行 ,同时break 可以接受一个可选的数字参数来决定跳出几重循环(用于多重循环)。

示例:

<?php

for($i = 0; $i < 10; $i++){
    if($i == 5){
        break;
    }
    echo &#39;$i的值是&#39; . $i . &#39;<br>&#39;;
}
Copy after login

结果:
PHP Basic Tutorial 4 - Process Control

流程图:
PHP Basic Tutorial 4 - Process Control

从结果中可以看到,当$i的值等于5的时候,就break,跳出循环,所以输出语句输出了5句。

continue

continue在循环结构中用来跳过本次循环,然后接着进行判断是否满足条件。注意:是跳出单次循环,而break是跳出整个循环结构。
continue 可接受一个可选的数字参数来决定跳过几重循环到循环结尾

示例:

<?php

for($i = 0; $i < 10; $i++){
    if($i == 5){
        continue;
    }
    echo &#39;$i的值是&#39; . $i . &#39;<br>&#39;;
}
Copy after login

结果:
PHP Basic Tutorial 4 - Process Control

流程图:
PHP Basic Tutorial 4 - Process Control

在流程图中可以看到当满足$i == 5的时候,continue跳出本次循环,这时下面的输出就不能输出,所以看到在结果中没有输出$i == 5这种情况。

goto

goto操作符可以用来跳转到程序中的另一位置。该目标位置可以用目标名称加上冒号来标记。

示例:

<?php

$a = 12;
goto A;
echo &#39;这是第一个位置&#39;;
A:
echo &#39;这是第二个位置&#39;;
.....结果......
这是第二个位置
Copy after login

可以看到其中一个输出语句没有输出,这是因为goto跳转到了A这个位置,注意A后面是一个冒号:
同时goto可以使用在循环中,这样也可以跳出循环。
使用注意事项:

  1. 使用注意事项: PHP中的goto有一定限制,只能在同一个文件和作用域
    中跳转,也就是说你无法跳出一个函数或类方法,也无法跳入到另一个函数。

  2. 常见的用法是用来跳出循环或者switch,可以代替多层的break

总结

流程控制,在开发中不可避免的,我们写的代码总是在流程控制中执行,明白了PHP的流程控制也就明白了代码的整体框架。同时for循环while循环等自己可以进行加深,不管什么编程语言,只有自己不停地敲代码才会明白其中的道理。所以要多敲代码,多练习。

 以上就是PHP基础教程四之流程控制的内容,更多相关内容请关注PHP中文网(www.php.cn)!


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