Home arrow PHP arrow Page 4 - Mastering WHILE Loops for PHP and MySQL

Constructing Trigonometric Tables Using WHILE Loops - PHP

Do you want to learn how to handle PHP WHILE loops? WHILE loops are one of the most powerful features as well as the easiest loop available to any PHP/MySQL developer. They enable us to shorten repetitive tasks for a highly useful application. This tutorial gives examples of WHILE loops in PHP/MySQL that beginner and novice developers can use as a quick reference for building similar loops in their applications.

TABLE OF CONTENTS:
  1. Mastering WHILE Loops for PHP and MySQL
  2. Using WHILE Loops as Counters
  3. Generating Unique Random Numbers Using WHILE Loops
  4. Constructing Trigonometric Tables Using WHILE Loops
  5. Retrieving All Rows in a MySQL Table Using WHILE Loops
By: Codex-M
Rating: starstarstarstarstar / 9
October 27, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Have you ever thought of constructing trigonometric tables? To construct a table for an angle starting from 0 degrees and continuing to 360 degrees using three basic trigonometric functions and their inverses (with $x as the variable):

sin(deg2rad($x))

cos(deg2rad($x))

tan(deg2rad($x))

You will need to iterate going from 0 degrees to 360 degrees using a WHILE loop. You will need to convert $x to radians, since by default trigonometric functions evaluate angles as radians. You can accomplish this using the function:

Deg2rad();

Below is the complete script required to generate the trigonometric table for the three functions (sine, cosine and tangent) and well as their inverses (cotangent, secant, and cosecant)

<?php

echo '<table width=100% border=1>';

echo '<tr><td><b>Angle in Degrees</b></td><td><b>Sine</b></td><td><b>Cosine</b></td>
<td><b>Tangent</b></td><td><b>Cotangent</b></td>
<td><b>Secant</b></td><td><b>Cosecant</b></td></tr>';

$i=1;

while ($i<=360) {

$x=$i++;

echo '<tr>';

echo '<td>'.$x.'</td>';

echo '<td>'.sin(deg2rad($x)).'</td>';

echo '<td>'.cos(deg2rad($x)).'</td>';

echo '<td>'.tan(deg2rad($x)).'</td>';

echo '<td>'.(1/tan(deg2rad($x))).'</td>';

echo '<td>'.(1/cos(deg2rad($x))).'</td>';

echo '<td>'.(1/sin(deg2rad($x))).'</td>';

echo '</tr>';

}

echo '</table>';

?>

The screen shot below is the trigonometric table when viewed in a browser: 

You can also extend the above application not only to trigonometry but to other important engineering tables like logarithmic tables, steam tables, etc.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: