PHP flow contro...LOGIN

PHP flow control if statement

In order to enhance everyone's understanding of the code, we put together a story to prank a classmate Wang Sixong.

In both chapters 4.1 and 3.2.5 we introduced the if and if...else structures. And we explained it very clearly.

Let's now use the if...else structure to write a small thing to enhance everyone's understanding of logic.

We will write a calculator based on the previous knowledge points:

<form>
    <input type="text" name="num1">

    <select name="fh">
        <option value="jia"> + </option>
        <option value="jian"> - </option>
        <option value="c"> x </option>
        <option value="chu"> / </option>
        <option value="qy"> % </option>

    </select>

    <input type="text" name="num2">

    <input type="submit" value="运算" />


</form>

<?php

    $num1 = $_GET['num1'];
    $num2 = $_GET['num2'];
    $fh = $_GET['fh'];

    if(!is_numeric($num1) || !is_numeric($num2)){

        echo '请输入数值类型';
    }

    if($fh == 'jia'){
        echo $num1 . '+' . $num2 . '=' . ($num1+$num2);
    }

    if($fh=='jian'){
        echo $num1 . '-' . $num2 . '=' . ($num1-$num2);
    }

    if($fh=='c'){
        echo $num1 . 'x' . $num2 . '=' . ($num1*$num2);
    }
    if($fh=='chu'){
        echo $num1 . '/' . $num2 . '=' . ($num1/$num2);
    }
    if($fh=='qy'){
        echo $num1 . '%' . $num2 . '=' . ($num1%$num2);
    }

?>

Assignment:
Write a calculator for ordinary years and leap years. Write a form, pass the year through get, and determine whether the year passed in is a numeric type. And it is required that if it is a leap year, it will prompt that it is a leap year, and if it is an ordinary year, it will indicate that this year is an ordinary year.

Leap year rules for ordinary years: The year can be evenly divisible by 4, but not by 100. Or if it is divisible by 400, it is a leap year, otherwise it is an ordinary year

Next Section
<form> <input type="text" name="num1"> <select name="fh"> <option value="jia"> + </option> <option value="jian"> - </option> <option value="c"> x </option> <option value="chu"> / </option> <option value="qy"> % </option> </select> <input type="text" name="num2"> <input type="submit" value="运算" /> </form> <?php $num1 = $_GET['num1']; $num2 = $_GET['num2']; $fh = $_GET['fh']; if(!is_numeric($num1) || !is_numeric($num2)){ echo '请输入数值类型'; } if($fh == 'jia'){ echo $num1 . '+' . $num2 . '=' . ($num1+$num2); } if($fh=='jian'){ echo $num1 . '-' . $num2 . '=' . ($num1-$num2); } if($fh=='c'){ echo $num1 . 'x' . $num2 . '=' . ($num1*$num2); } if($fh=='chu'){ echo $num1 . '/' . $num2 . '=' . ($num1/$num2); } if($fh=='qy'){ echo $num1 . '%' . $num2 . '=' . ($num1%$num2); } ?>
submitReset Code
ChapterCourseware