PHP Number Tips - Part 17: You want to perform a trigonometric calculation such as finding the sine or cosine of an angle. So, in order to do this, you have to use PHP's numerous trigonometric functions:
<?php
//define angle
$angle = 45;
//calculate sine
//result: "Sine: 0.850903524534 "
$sine = sin($angle);
echo "Sine: $sine \n";
//calculate cosine
//result: "Cosine: 0.525321988818 "
$csine = cos($angle);
echo "Cosine: $csine \n";
//calculate tangent
//result: "Tangent: 1.61977519054 "
$tangent = tan($angle);
echo "Tangent: $tangent \n";
//calculate arc sine
//result: "Arc sine: -1.#IND "
$arcSine = asin($angle);
echo "Arc sine: $arcSine \n";
//calculate arc cosine
//result: "Arc cosine: -1.#IND "
$arcCsine = acos($angle);
echo "Arc cosine: $arcCsine \n";
//calculate arc tangent
//result: "Arc tangent: 1.54857776147 "
$arcTangent = atan($angle);
echo "Arc tangent: $arcTangent \n";
//calculate hyperbolic sine
//result: "Hyperbolic sine: 1.74671355287E+019 "
$hypSine = sinh($angle);
echo "Hyperbolic sine: $hypSine \n";
//calculate hyperbolic cosine
//result: "Hyperbolic cosine: 1.74671355287E+019 "
$hypCsine = cosh($angle);
echo "Hyperbolic cosine: $hypCsine \n";
//calculate hyperbolic tangent
//result: "Hyperbolic tangent: 1 "
$hypTangent = tanh($angle);
echo "Hyperbolic tangent: $hypTangent \n";
?>
PHP comes with a rich toolkit of functions designed specifically to assist in trigonometry and with this functions, you can calculate sines, cosines, and tangents for any angle. While there aren't yet built-in functions to calculate secants, cosecants, and cotangents, it's fairly easy to calculate these inversions with the functions that are available.