非常基本的PHP计算器:can';I don’我不知道;I’我做得不对


Very basic PHP calculator: can't work out what I'm doing incorrectly

在开始学习PHP时,我只是在做一个相当蹩脚的计算器。我不明白为什么除了答案不会显示之外,一切似乎都很好。

这是HTML:

<head>
<meta charset="utf-8">
<title>A (Seriously) Simple Calculator</title>
<link rel="stylesheet" type="text/css" href="./calc_css.css">
</head>
<body>
<form method="post" attribute="post" action="calc1.php">
<p>First Value:<br/>
<input type="number" id="first" name="first" step="0.0000000001"></p>
<p>Second Value:<br/>
<input type="number" id="second" name="second" step="0.0000000001"></p>
<p>+<input type="radio" name="operation" id="add" value="add" checked="true"></p><br/>
<p>-<input type="radio" name="operation" id="subtract" value="subtract"></p><br/>
<p>X<input type="radio" name="operation" id="multiply" value="multiply"></p><br/>
<p>/<input type="radio" name="operation" id="divide" value="divide"></p><br/>
<p></p>
<button type="submit" name="answerswer" id="answerswer" value="answerswer">Calculate</button>
</form>
</body>
</html>

这是我的PHP:

<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is: 
<?php
$first = floatval($_POST['first']);
$second = floatval($_POST['second']);
if($_POST['operation'] == 'add') {
echo $first + $second;
}
else if($_POST['operation'] == 'subtract') {
echo $first - $second;
}
else if($_POST['operation'] == 'multiply') {
echo $first * $second;
}
else($_POST['operation'] == 'divide') {
echo $first / $second;
}
?>
</p> 
</body>
</html>

我认为这与我的输入步骤或类型无关,我已经在我的PHP文件中尝试了各种各样的东西(我能想到的)。也就是说,我是一个非常环保的初学者。如有任何帮助,我们将不胜感激。

问题是最后一个条件语句是else,而不是另一个else if

else($_POST['operation'] == 'divide') {
echo $first / $second;
}

如果将错误报告设置为捕获和显示,就会抛出这样的东西:

分析错误:语法错误,第24行/var/usr/you/folder/file.php中出现意外的"{"

更改为:

else if($_POST['operation'] == 'divide') {
echo $first / $second;
}

错误报告添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code

旁注:显示错误只能在暂存中进行,而不能在生产中进行。

  • 无论选择哪个操作,都将抛出该通知。

  • 如果某个东西没有设置或符合比较语句中的标准,如ifelse if:,则else{...}用作出口

  • http://php.net/manual/en/control-structures.if.php.

假设else($_POST['operation'] == 'divide')总是与"除法"进行比较,而不是else { $var=x; }是"将此变量分配给"x",从而导致错误。

根据手册:http://php.net/manual/en/control-structures.elseif.php

<?php
if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}
?>

其他参考文献:

  • http://php.net/manual/en/language.operators.comparison.php
  • http://php.net/manual/en/language.operators.assignment.php

您的代码中有一个错误,请查看PHP代码中的最后一条"else"语句。

<?php
    if($_POST['operation'] == 'add') {
        echo $first + $second;
    }
    else if($_POST['operation'] == 'subtract') {
        echo $first - $second;
    }
    else if($_POST['operation'] == 'multiply') {
        echo $first * $second;
    }
    else if($_POST['operation'] == 'divide') {
        if($second == 0){
            echo 'Cannot divide by 0';
        } else {
            echo $first / $second;
        }
    } else {
        echo 'unknown operator';
    }
?>

还要记住,不能除以0,因为它会在PHP中创建一条警告消息。

因此,在编写和测试"Fred-ii-"答案中显示的代码时,最好启用error_reporting。

相关文章: