dongtang1966 2015-11-22 23:51
浏览 6
已采纳

非常基本的PHP计算器:无法弄清楚我做错了什么

I'm just doing a rather lame calculator as I am beginning to learn PHP. I can't figure out why everything seems to be fine except the answer will not display.

Here's the 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="answer" id="answer" value="answer">Calculate</button>
</form>
</body>
</html>

And here's my 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>

I don't think it has to do with my input step or type, and I've tried all manner of things (that I can think of) in my PHP file. That said, I am a very green beginner. Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • drnmslpz42661 2015-11-23 00:01
    关注

    The problem is with your last conditional statement being else instead of another else if.

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

    Having error reporting set to catch and display, would have thrown you something like this:

    Parse error: syntax error, unexpected '{' in /var/usr/you/folder/file.php on line 24

    Change it to:

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

    Add error reporting to the top of your file(s) which will help find errors.

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

    Sidenote: Displaying errors should only be done in staging, and never production.

    It's assuming that else($_POST['operation'] == 'divide') will always compare to "divide" rather than else { $var=x; } being an "assign this variable to "x", in turn throwing you an error.

    From the manual: 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";
    }
    ?>
    

    Other references:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测