This will eventually turn into a complete trigonometry tutorial. I am at school now so it might be a few weeks away.
Starting out with Trigonometry in PHP
Aim: To get a user to enter a value and a script will determine the cosine, sine and tangent.
Hypothesis: A user will enter a value between 1-90 and it will be converted to radians so it can be calculated correctly. The script will then output an answer to the chosen radio button, cosine, sine and tangent. In other words save them from using a calculator or an image like;
[img] http://upload.wikimedia.org/wikipedi..._functions.svg [/img]
Procedure:
Let’s get started by creating the interface the user will be using. I won’t be explaining this since you should have a knowledge of HTML.
Here is the code for it:
PHP Code:
<?php
function formInput() {
?>
<center>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
Please input the number you wish to work with. Make sure it is in degrees!
</p>
<p>
<input type="text" name="val" />
</p>
<p>
<input type="radio" name="opt" value="sin" /> Sine (sin) <br />
<input type="radio" name="opt" value="cos" /> Cosine (cos) <br />
<input type="radio" name="opt" value="tan" /> Tangent (tan)
</p>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</center>
<?php
}
?> Just a brief description on the PHP section of this. The first part we are creating a function called formInput, something you might find quite unusual is that the opening tag is at the top of the file in a separate set of PHP tags to the closing tag. The reason for this is to make sure PHP outputs the HTML correctly you have to use;
PHP Code:
Echo “HTML HERE”;
Now the second part is with the action of the form. In a quick summary all it is doing is telling that the action of the form is to use itself. So instead of using another file it will use its own.
Next is the hardest part, yet really easy, of the script. The functions which will do the magic for us all, remember these functions have no use till they are used. This will be shown in the section of this tutorial. Here is the script;
PHP Code:
<?php
function sinAns($val) {
if ($val > 360 || $val <= 0) {
echo "Allowed number are only between 1 and 360.";
} else {
$ans = sin(deg2rad($val));
echo $ans;
}
}
function cosAns($val) {
if ($val > 360 || $val <= 0) {
echo "Allowed number are only between 1 and 360.";
} else {
$ans = cos(deg2rad($val));
echo $ans;
}
}
function tanAns($val) {
if ($val > 90 || $val <= 0) {
echo "Allowed number are only between 1 and 90.";
} else {
$ans = tan(deg2rad($val));
echo $ans;
}
}
?> Now let’s get on with one of the functions that do the calculations.
PHP Code:
function sinAns($val) {
if ($val < 90 || $val >= 0) {
echo "$opt can only be used with a number between 1 and 90.";
} else {
echo sin(deg2rad($val));
}
}
Now save this file as functions.php. Next is the file which processes all the functions and values.
Create a new PHP file and save it as index.php. This is the main part of the code. Here it is;
PHP Code:
<?php
require "functions.php";
require "form.php";
if(!isset($_POST['submit'])) {
formInput();
} else {
switch ($opt) {
case "sin":
sinAns($val);
break;
case "cos":
cosAns($val);
break;
case "tan":
tanAns($val);
break;
}
echo "<br /><a href='index.php'>Another calculation</a>";
}
?> The rest of the code is an if statement. If the user has not clicked on the submit button, it will do the function “formInput()”. As you can see we typed this in “form.php” a while ago which is the interface for the user. If they have clicked on it, it will do a switch statement. The switch statement is based on the value of $opt. Each case is a possibility of what it can be. So let’s say that the user chose “sin” it will do the function sinAns using $val as the value to be used. The break statement then closes that case.
The last echo statement gives the user to do another calculation.
discuss this topic to forum
