0
在PHP編寫了一個二次方程計算器後,我想我的大部分問題都會與數學有關。儘管如此,因爲我正在得到非常奇怪的輸出。程序應該是$_GET
這個x的值,這個x和其他數字的值是從一個表格中計算出來的,然後顯示它們。當我第一次加載頁面時,程序輸出-10
(即使我沒有在表單中輸入任何內容),如果輸入值,則什麼也不做。例如,如果我在輸入x = -9 and -2
的文本字段中輸入1, 11 and 18
,則程序輸出-22
。我究竟做錯了什麼?PHP二次方程計算器奇怪的輸出
這裏是我的代碼(我的HTML文檔的<body>
部分):
<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>
什麼是錯與數學位?另外,謝謝你,因爲它的作品! – imulsion 2013-03-09 11:00:43
但爲什麼使用'echo'而不是'print'? – imulsion 2013-03-09 11:03:15
我編輯了我的評論,並添加了數學位。關於'echo'和'print',我更喜歡'echo'來輸出簡單的內容(大多數人都這樣做) – 2013-03-09 11:05:57