gpt4 book ai didi

php - 如何删除 php 中的零除消息并改进我的代码?

转载 作者:行者123 更新时间:2023-12-05 05:57:28 24 4
gpt4 key购买 nike

你好我是一个完全的初学者,我只知道 Html、CSS 和 PHP 的基础知识,我能做些什么来解决这个问题(除以零错误和 https://prnt.sc/1p4kw8a)并改进我的代码。第二个问题是当我用空文本框提交它时很明显提前谢谢你

<!DOCTYPE HTML>
<html>
<head> <meta charset="utf-8">

<title> Calculator </title>
</head>
<body>
<form action="calculator.php" method="post">
Firt number - <input type="number" step="0.1" id="number1" name="number1"><br>
Second number - <input type="number" step="0.1" id="number2" name="number2"><br>
Option - <select name="operator">
<option value="add" name="gruppe1">+</option>
<option value="subtract" name="gruppe1">-</option>
<option value="times" name="gruppe1" >x</option>
<option value="divide" name="gruppe1">/</option>
<option value="caret" name="gruppe1">^</option>
</select><br>
<input type="submit">
</form>

<?php
$gruppe1 = $_POST["operator"];
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
if($_POST["operator"] == "add"){
echo $number1 + $number2;
} elseif($_POST["operator"] == "subtract"){
echo $number1 - $number2;
} elseif($_POST["operator"] == "times"){
echo $number1 * $number2;
} elseif($_POST["operator"] == "divide"){
echo $number1 / $number2;
} elseif($_POST["operator"] == "caret"){
echo $number1 ** $number2;
}




?>


</body>
</html>

最佳答案

<!DOCTYPE HTML>
<html>
<head> <meta charset="utf-8">
<title> Calculator </title>
</head>
<body>
<form action="calculator.php" method="post">
Firt number - <input type="number" step="0.1" id="number1" name="number1"><br>
Second number - <input type="number" step="0.1" id="number2" name="number2"><br>
Option - <select name="operator">
<option value="add" name="gruppe1">+</option>
<option value="subtract" name="gruppe1">-</option>
<option value="times" name="gruppe1" >x</option>
<option value="divide" name="gruppe1">/</option>
<option value="caret" name="gruppe1">^</option>
</select><br>
<input type="submit">
</form>
<?php
$gruppe1 = $_POST["operator"];
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
if($_POST["operator"] == "add"){
echo $number1 + $number2;
} elseif($_POST["operator"] == "subtract"){
echo $number1 - $number2;
} elseif($_POST["operator"] == "times"){
echo $number1 * $number2;
} elseif($_POST["operator"] == "divide"){
echo $result = $number2 != 0 ? $number1 / $number2 : "Error";
} elseif($_POST["operator"] == "caret"){
echo $number1 ** $number2;
}
?>
</body>

关于php - 如何删除 php 中的零除消息并改进我的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68791691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com