Home  >  Article  >  Backend Development  >  Learn the three major process controls of PHP through classic examples

Learn the three major process controls of PHP through classic examples

伊谢尔伦
伊谢尔伦Original
2017-06-22 10:28:501829browse

1. Sequence control:
By default, a simple sequence is executed from left to right and top to bottom. Uncontrolled process.

2. Branch control:
Single branch, double branch, multiple branches;
Single branch syntax:

$age=17;
if ($age<18) {
    echo "未满十八岁未成年";
}

Double branch syntax:

$age=24;
if ($age<18) {
    echo "未满十八岁未成年";
} else {
    echo "您已满十八岁符合标准";
}

Multiple branches Grammar: (elseif can have multiple elses or none. switch statement can also be used for multi-branch judgment)

if ($age<18) {
    echo "未满十八岁未成年不符合标准";
} elseif ($age>60) {
    echo "超过六十岁不符合标准";
} else {
    echo "花好年华!";
}

switch is also a common usage of multi-branch statements. Note:
Each case must be followed by a break interrupt switch, and the default is optional depending on the needs. The location of the default has no impact on the execution result. First, it is executed in the order of the cases, and the default content is executed when none of them match. Write the condition expression in the switch, and the constant after the case (decimals are also acceptable).

$day="1";
switch ($day) {
    case &#39;1&#39;:
        echo "今天星期一";
        break;
    case &#39;2&#39;:
        echo "今天星期二";
        break;
    case &#39;3&#39;:
        echo "今天星期三";
        break;    

    default:
        echo "地球还在转但是不知星期几";
        break;
    }

Analysis of usage scenarios of if and switch:
if is a judgment on a certain range, while switch is a judgment on a point, when the branch is several points (such as judging the direction of a tank , gender), you should use switch. If the branch is a judgment of several areas (range), consider using if. switch is more efficient.

三、loopControl:
for loop,while,do while

//for循环
for ($i=0; $i < 18; $i++) { 
    echo "<br/>GoodGoodStudy";
}
//while
$i=0;//循环控制变量
while ($i<10) {
    echo "<br/>你好长沙";
    $i++;//这里对循环控制变量自增
}
//do while
    $i=0;//循环控制变量
    do {
        echo "<br/>你好长沙";
        $i++;
    } while($i<10);

Printing half of the pyramid exercise:

//金字塔的一半
    for ($i=0; $i < 5; $i++) {//外层循环控制层数
        for ($j=0; $j <= $i; $j++) { //内层循环控制每层个数
            echo "*";
        }
        echo "<br/>";
    }

Learn the three major process controls of PHP through classic examples


##Print Pyramid Exercise:

//金字塔
for ($i=0; $i <= 5; $i++) { 
    for ($k=0; $k < 5-$i; $k++) {//打*前打空格
        echo "&nbsp";
    }
    for ($j=0; $j<($i-1)*2+1; $j++) {//每行多少*的规律:($i-1)*2-1
        echo "*";
    } 
    echo "<br/>";
}

Learn the three major process controls of PHP through classic examples


Printing Hollow Pyramid Exercise:

//空心金字塔
$n=5;
for ($i=1; $i <= $n; $i++) { 
    for ($k=1; $k <= $n-$i; $k++) {//打*前打空格
        echo "&nbsp";
    }
    for ($j=1; $j<=($i-1)*2+1; $j++) {//每行多少*的规律:($i-1)*2-1
        if ($i==1 || $i==$n) {
            echo "*";
        } else {
            if ($j==1 || $j==($i-1)*2+1) {
                echo "*";
            } else {
                echo "&nbsp";
            }
        }
    } 
    echo "<br/>";
}

Learn the three major process controls of PHP through classic examples


##Calculator Exercise:

Calculator Interface:

<!DOCTYPE html>
<html>
<head>
    <title>我的计算器</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>

<form action="CalResult.php" method="post">
    <table width="300px" border="0">
        <tr>
            <td>第一个数</td>
            <td><input type="text" name="number1"></td> 
        </tr>
        <tr>
            <td>第二个数</td>
            <td><input type="text" name="number2"></td>
        </tr>

        <tr>
            <td>运算符</td>
            <td>
                <select name="oper">
                    <option value="+">+</option>
                    <option value="-">-</option>
                    <option value="*">*</option>
                    <option value="/">/ </option>
                </select>
            </td>
        </tr>

        <tr>
            <td colspan="2"><input type="submit" value="计算结果"></td>
        </tr>
    </table>
</form>
</body>
</html>

Learn the three major process controls of PHP through classic examples
Calculation result:

<?php

    //接收用户从Calculator.php(对应静态页面 浏览器)提交的数据
    //1、接收number1 number2 ;REQUEST方法可以接收用户的post或者get请求数据
    $number1=$_REQUEST[&#39;number1&#39;];
    $number2=$_REQUEST[&#39;number2&#39;];
    //接收运算符
    $oper=$_REQUEST[&#39;oper&#39;];

    $res=0;
    switch ($oper) {
        case &#39;+&#39;:
            $res=$number1+$number2;
            break;
        case &#39;-&#39;:
            $res=$number1-$number2;
            break;
        case &#39;*&#39;:
            $res=$number1*$number2;
            break;
        case &#39;/&#39;:
            $res=$number1/$number2;
            break;
        default:
            echo "运算符不正确";
            break;
    }
    echo "运算结果是:".$res;
?>
<a href="Calculator.php">返回计算器页面</a>

Learn the three major process controls of PHP through classic examples

The above is the detailed content of Learn the three major process controls of PHP through classic examples. For more information, please follow other related articles on the PHP Chinese website!

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