Modulo和Division操作员即使没有连接也可以一起工作

时间:2018-10-03 00:47:38

标签: php

取模和除法之间有两个选择,但是无论选择哪个,都会显示两个输出

if ($_POST['op'] == divide && $num1 == 0) {
                    echo "Sorry, you can't divide by zero.";
            } else {
        echo "$num1 divided by $num2 equals $divideresult";
    } if ($_POST['op'] == remainder) {
        echo "When $num1 is divided by $num2, the remainder is $remresult";

输出为“ 0除以1等于0当0除以1时,即使我仅选择取模而不除法,余数为0。

1 个答案:

答案 0 :(得分:0)

输出为“ 0除以1等于0当0除以1时,即使我仅选择取模而不选择除法,余数为0。

因为这是执行方式。当您选择divide选项时,它将转到else的{​​{1}}块,因为它不匹配,因此回显divide和其他echo "0 divided by 1 equals 0"条件相同级别,然后检查if条件并回显remainder

这是执行方式。

When 0 is divided by 1, the remainder is 0

我猜应该是这样

<?php
if ($_POST['op'] == 'divide' && $num1 == 0) {
    echo "Sorry, you can't divide by zero.";
} else {
    echo "$num1 divided by $num2 equals $divideresult";
} 

// this condition also get executed every time though you want to check for divide because both divide and remainder are on same level
if ($_POST['op'] == 'remainder') {
    echo "When $num1 is divided by $num2, the remainder is $remresult";
    //your other code goes here
}    
?>