用 PHP 编写了一个二次方程计算器后,我认为我的大部分数学问题都会遇到。但并非如此,因为我得到了非常奇怪的输出。该程序应该从表单$_GET
中获取 x 2、 x 和其他数字的值,计算 x 的值,然后显示它们。当我第一次加载页面时,程序会输出-10
(即使我没有在表单中输入任何内容),然后如果我输入值则什么也不做。例如,如果我输入1, 11 and 18
应该输出的文本字段,x = -9 and -2
程序输出-22
。我究竟做错了什么?
这是我的代码(<body>
我的 HTML 文档的部分):
<body>
<h1>Quadratic equation calculator</h1>
<p>Type the values of your equation into the calculator to get the answer.</p>
<?php
$xsqrd;
$x;
$num;
$ans1 = null;
$ans2 = null;
$errdivzero = "The calculation could not be completed as it attempts to divide by zero.";
$errsqrtmin1 = "The calculation could not be completed as it attempts to find the square root of a negative number.";
$errnoent = "Please enter some values into the form.";
?>
<form name = "values" action = "calc.php" method = "get">
<input type = "text" name = "x2"><p>x<sup>2</sup></p>
<input type = "text" name = "x"><p>x</p>
<input type = "text" name = "num">
<input type = "submit">
</form>
<?php
if ((!empty($_GET['x2'])) && (!empty($_GET['x'])) && (!empty($_GET['num']))) {
$xsqrd = $_GET['x2'];
$x = $_GET['x'];
$num = $_GET['num'];
$ans1 = (-$x) + (sqrt(pow($x, 2) - (4 * $xsqrd * $num))) / (2 * $xsqrd);
$ans2 = (-$x) - (sqrt(pow($x, 2) - (4 * $xsqrd * $num))) / (2 * $xsqrd);
}
?>
<p>
<?php
if(($ans1==null) or ($ans2==null))
{
print $errnoent;
}
else
{
print "x = " + $ans1 + "," + $ans2;
}
?>
</p>
</body>