如果其中两个为真,两个错误链接都会出现,并且消息会叠加在一起,如果一个input is 0 if验证空输入的语句也会出现,如果input不是数字,但另一个输入是数字,php仍然会计算,但只是返回你给它的任何数字加上错误代码,我该如何修复?
第一条语句是验证是否有任何值为0,以及操作是否被除以产生错误
if ($first == 0)
{ if ($operation == "Division")
print("<h1>ERROR</h1>
<p class=\"message\">YOU CAN NOT DIVIDE BY ZERO</p>");
}
else if($second == 0)
{ if ($operation =="Division")
print("<h1>ERROR</h1>
<p class=\"message\"> YOU CAN NOT DIVIDE BY ZERO</P>");
}此if语句用于验证输入是否为数字
if (!is_numeric($first) or !is_numeric($second))
{
print("<h1> Error one or more inputs are not numbers</h1>" . "<p class=\"backlink\"><a href=\"calculator.html\">Go Back</a></p>");
}第三个if语句用于验证正数
else if($first < 0 or $second < 0)
{
print("<h1> Error one of more of your numbers is a negative</h1>" . "<p class=\"backlink\"><a href=\"calculator.html\">Go Back</a></p>");
}此if语句验证空输入
if (empty($first) or empty($second))
{
print("<p class=\"message\"> One or more input field is empty </p>" . "<p class=\"backlink\"><a href=\"calculator.html\">Go Back</a></p>");
}else{发布于 2019-12-03 15:17:07
@irvdtcc查看代码,我知道你需要检查多个条件,只有当所有条件都为真时才会显示结果。
要检查的条件1) $first和$second是大于0的数字2) $first和$second输入不为空。
BEst的方法是为每个条件使用标志,如下所示。
<?php
$fl_positive=0;
$fl_num=0;
$fl_empty=0;
$error_message="";
//check if number is empty -- empty is true of $first=0 or $first="". In either case //empty($first) will result in true.
if(empty($first) or empty($second)){
$fl_empty=1;
$error_emssage . = "First or Second number is empty or zero";
}
elsif($first<0) or ($second<0)) {
$fl_positive=1;
$error_message . = " First or Second number is Negative";
}
//Check all criteria in the same manner
if($fl_positive==1) or ($fl_num==0) or ($fl_empty=0)){
echo $error_message;
}
?>https://stackoverflow.com/questions/59133337
复制相似问题