Home PHP 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.

By: Codex-M
Rating:  / 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

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap