取模和除法之间有两个选择,但是无论选择哪个,都会显示两个输出
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。
答案 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
}
?>