sincostan.php
程式碼如下:
<!doctype html>
<html>
<head>
<style>
.press { width:162px; }
.error { color:#F00; }
</style>
<meta charset="utf-8">
<title>三角函數</title>
</head>
<body>
<?php
$scaleErr="";
settype($scale,"double"); //scale變成double型態
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["scale"])) {
$scaleErr = "忘了寫倍數了";
}
else {
$scale=test_input($_POST["scale"]);
$scale=$scale*pi();
//倍數可以是小數或整數
}
}
function test_input($data) {
if(!is_string($data)) $data='scale';
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
請輸入倍數:<input type="text" name="scale" class="press">
<span class="error">* <?php echo $scaleErr;?></span>
<br><br><input type="submit" name="submit" value="送出"><br>
</form>
<?php
if($scale!=0){
echo "<br>";
echo "sin(".$scale.")=".sin($scale)."<br>";
echo "cos(".$scale.")=".cos($scale)."<br>";
echo "tan(".$scale.")=".tan($scale)."<br>";
echo "cot(".$scale.")=".(1/tan($scale))."<br>";
echo "sec(".$scale.")=".(1/cos($scale))."<br>";
echo "csc(".$scale.")=".(1/sin($scale))."<br>";
}
else {
$scale=@($scale==0);
echo "$php_errormsg";
}
?>
</body>
</html>
假設我倍數輸入0.666666666666666
結果如下:
sin(2.09439510239)=0.866025403784
cos(2.09439510239)=-0.5
tan(2.09439510239)=-1.73205080757
cot(2.09439510239)=-0.57735026919
sec(2.09439510239)=-2
csc(2.09439510239)=1.15470053838
結果正確
可是我輸入0
結果是* 忘了寫倍數了
拜託各位看看哪裡有問題
感激不盡