PHP简单计算器问题


PHP simple Calculator problems

我的PHP计算器有问题。基本上我需要2个输入,如果它们的总和等于并大于85,我想要它输出这个消息:"你符合85规则的要求",如果不是"你不符合85规则"。下面是我的代码:

<?php
if (isset($_POST['valuea'])) $valuea = $_POST['valuea'];
if (isset($_POST['valueb'])) $valueb = $_POST['valueb'];
$answer = $valuea + $valueb ;
 if ($answer > "84"){echo "You meet the requirements for the rule of 85";}else{echo "You do not meet the rule of 85";}
echo <<<_END
<form method='post' action='/make-a-website/online-calculator'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>Rule of 85 calculator </strong>         
</td></tr>
<tr class="calcrow"><td>Enter your years of work:</td><td align="center"><input type='text' name='valuea' value="$valuea"/></td></tr>
<tr class="calcrow2"><td>Enter your age:</td><td align="center"><input type='text' name='valueb' value="$valueb"/></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>
<tr class="calcrow">
<td><i>The answer is:</td>
<td align="center"><input type="text" value="<?php echo ($valuec)?>"></td></i>
</tr>
</table>
</form> 

试试这个:

    <?php
$valuea = (int)$_POST['valuea'];
$valueb = (int)$_POST['valueb'];
$answer = $valuea + $valueb ;
 if ($answer > 84){
       echo "You meet the requirements for the rule of 85";
 }else{
       echo "You do not meet the rule of 85";
}
?>
<form method='post' action='/make-a-website/online-calculator'>
    <table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
        <tr class="calcheading">
            <td colspan="2"><strong>Rule of 85 calculator </strong></td>
        </tr>
        <tr class="calcrow">
            <td>Enter your years of work:</td>
            <td align="center"><input type='text' name='valuea' value="<?php echo $valuea; ?>" /></td>
        </tr>
        <tr class="calcrow2">
            <td>Enter your age:</td>
            <td align="center"><input type='text' name='valueb' value="<?php echo $valueb; ?>" /></td>
        </tr>
        <tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
        <tr class="calcrow">
            <td><i>The answer is:</td>
            <td align="center"><input type="text" value="<?php echo $answer; ?>"></td>
        </tr>
    </table>
</form> 

首先,你应该在你的问题更具体,这样我们就知道要寻找什么。我发现了你的问题:

if ($answer > "84"){echo "You meet the requirements for the rule of 85";}else{echo "You do not meet the rule of 85";}

你的代码不能正常工作的原因是因为你正在比较你的$answer变量与字符串。把"84"放在引号里告诉PHP你想要一个字符串,而不是像int或float这样的数字。

将你想要使用算术运算的所有数字的实例替换为相同的数字减去引号,你的代码应该开始以更可预测的方式工作:)

修改如下:

if ($answer > "84")

:

if ($answer > 84)

前者检查字符串,后者检查整数。对于所有的意图和目的,字符串1基本上是无用的