PHP简单的计算器问题

时间:2014-06-30 15:24:24

标签: php

我的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> 

3 个答案:

答案 0 :(得分:1)

试试这个:

    <?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> 

答案 1 :(得分:-1)

首先,您应该在问题中更加具体,以便我们知道要寻找什么。我找到了你的问题:

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

您的代码无法正常工作的原因是您将$ answer变量与String进行比较。放置&#34; 84&#34;在引号中告诉PHP你想要一个字符串而不是一个数字,如int或float。

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

答案 2 :(得分:-1)

改变这个:

if ($answer > "84")

到此:

if ($answer > 84)

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