|
||||||
| PHP Tutorials PHP Tutorials |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
G'day everyone. This is my latest tutorial and will be CRUCIAL for you to understand it for when I do part II. All this does is return results for an angle supplied using the formulas sin, cos and tan.
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:
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:
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:
Now let’s get on with one of the functions that do the calculations. PHP Code:
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:
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. Conclusion: Now you have completely working sin, cos and tan calculator ![]() +rep if you found this helpful.
__________________
Starting Trig tutorial PHP | OO Calculator tutorial PHP | What inspires you? | Quadratic Expressions | Converting to Binary Last edited by Brandon W; 10-11-2008 at 06:09 PM. |
| Sponsored Links |
|
|
|
|||||
|
Some comments/questions:
It's important for people to be aware that EVERY programming language uses radian based trig functions (because of calculus and its relation to the sciences). As a result, to accommodate degrees, it is necessary to use a conversion like the one Brandon used. That said, why did you limit the angle to 1-90? Sin and Cos are defined for all angles, and it is easy to test for the values where Tan is undefined.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||||
|
So Sin and Cos can be from 0 - 360 degrees?
__________________
Starting Trig tutorial PHP | OO Calculator tutorial PHP | What inspires you? | Quadratic Expressions | Converting to Binary |
|
|||||
|
sin and cos are defined on ALL real numbers.
tan is defined for all x where x is not in the form (2n+1)pi/2, n an integer (pi/2, 3pi/2, 5pi/2, etc)
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
| Sponsored Links |
|
|
|
|||||
|
Nice work Brandon W! +rep (if you want contest points please PM me).
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages! |
|
|||||
|
No thanks +rep will do. I want to be Guru
![]() Thanks for that Winged, I will change the code now. I was only working with Right-Angled Triangles.
__________________
Starting Trig tutorial PHP | OO Calculator tutorial PHP | What inspires you? | Quadratic Expressions | Converting to Binary |
|
|||||
|
You'll get the circle-based definition of the trig functions soon
![]()
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||||
|
Trig with Circles/Waves
I would +rep you for this, but I can't atm. I did for something else, and have to wait.
__________________
Emo Philips - "My computer beat me at checkers, but I sure beat it at kickboxing." |
|
|||||
|
Doesn't bother me mate it's all good
![]() Hmmm, that should be this term of Maths. We are doing Algebra 3 and Deductive Geometry Sounds interesting, I can't wait! We have started Algebra 3 just not the Geometry section.
__________________
Starting Trig tutorial PHP | OO Calculator tutorial PHP | What inspires you? | Quadratic Expressions | Converting to Binary |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|