Php 发生错误时仅显示一条HOW错误消息

Php 发生错误时仅显示一条HOW错误消息,php,Php,我正在网上创建一个计算器来挑战我的思考能力,挑战我的创造力,我已经掌握了基本知识,它可以工作,但我遇到的问题是,当没有错误时显示错误,只是在页面加载时显示,然后在使用计算器时显示值。有人能帮忙吗 <?php $value1 = $_POST['value1']; $symbol = $_POST['operator']; $value2 = $_POST['value2']; $nulr = "nulrl"; if($nulr === "nulrl"){ if($symbol ===

我正在网上创建一个计算器来挑战我的思考能力,挑战我的创造力,我已经掌握了基本知识,它可以工作,但我遇到的问题是,当没有错误时显示错误,只是在页面加载时显示,然后在使用计算器时显示值。有人能帮忙吗

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

编辑 这是我的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>

计算器
目标
通过对编码语言的研究,我逐渐认识到,几乎每一种编码语言,无论其语法如何不同,都可以处理用户输入和变量,产生和输出。说到这里,我想为每种编码语言制作一个交互式计算器,然后进一步研究每种语言,看看哪种语言能产生最好的计算器

PHP计算器
+ - * /

评论框
评论

编辑:
仍然没有收到任何正确答案

请按以下方式输入代码:

$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 : '';
如果出现任何错误,它将仅在post后显示。

最简单的方法:

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

如果没有,您将得到一个未定义的变量warningStill,加载age后立即显示错误消息不可能…此代码仅在提交表单时执行。如果你修改了代码…你也在使用js吗?到目前为止,还没有javascript编写了这个$http_post=($_SERVER['REQUEST_METHOD']='post');如果($http_post)?是,因为您没有添加条件(表单提交):您必须将以下内容放在PHP代码的第一行: