发生错误时只有错误消息

时间:2015-09-26 02:23:57

标签: php

我正在网上创建一个计算器,以挑战我的挑战能力,我的创意优势,我得到了基础知识,但我遇到的问题是错误显示没有错误只是显示时页面加载,然后显示计算器使用时的值,有人可以帮忙吗?

<?php

$value1 = $_POST['value1'];
$symbol = $_POST['operator'];
$value2 = $_POST['value2'];
$nulr = "nulrl";

if($nulr === "nulrl"){
if($symbol === "+"){
$output = $value1 + $value2;
} elseif($symbol === "-"){
$output = $value1 - $value2;
} elseif($symbol === "/"){
$output = $value1 / $value2;
} elseif($symbol === "*"){
$output = $value1 * $value2;
} else{
$output = "Error could not perform operation";
}

}
echo $output;
?>

EDIT 这是我的HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
<title>The Calculator</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>The Goal</h1>
<p>Through my studies of coding languages i have come to realize that  just about every coding langauage no matter how different the syntax can  handle user input and variables and produce and output. With this being said i wanna make an interactive calculator for each coding language and then advance on each one and see which language can produce the best  calculator!</p>

<main>
<h2>PHP Calculator</h2>
<form method="post">
<input type="number" name="value1" value="value1" class="number-box">
<br>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>    
<option value="*">*</option>
<option value="/">/</option>
</select>
<br>
<input type ="number" name="value2" value="value2" class="number-box">
<br>
<input type ="submit" name="submit" class="submit-button">
</form>
<?include("calculator.php")?>

<footer>
<h3>Comment Box</h3>
<form method="post" action="comment.php">
<input type="text" value="name" name="name">
<br>
<textarea name="comment">Comments</textarea>
<input type="submit" name="submit" class="submit-button">
</form>

<?php
echo file_get_contents("comments.txt");
?>
</footer>


</main>
</header>

   

编辑: 还没有收到任何正确的答案

2 个答案:

答案 0 :(得分:0)

像这样输入你的代码:

$http_post = ($_SERVER['REQUEST_METHOD'] == 'POST') ;
if($http_post)
{
        $value1 = $_POST['value1'];
        $symbol = $_POST['operator'];
        $value2 = $_POST['value2'];
        $nulr = "nulrl";

        if($nulr === "nulrl"){
        if($symbol === "+"){
        $output = $value1 + $value2;
        } elseif($symbol === "-"){
        $output = $value1 - $value2;
        } elseif($symbol === "/"){
        $output = $value1 / $value2;
        } elseif($symbol === "*"){
        $output = $value1 * $value2;
        }
        else
        {
            $output = "Error could not perform operation";
        }
}

echo (!empty($output)) ? $output : '';

如果发生任何错误,它将仅在发布后显示。

答案 1 :(得分:0)

最简单的方法:

<?php 
  if(isset($_POST['value1']){
    //.......
    else{
        $output = '';
        echo "Error, could not perform operation";
   } 
} 
echo $output;
}
?>