PHP Math Functions
Here you will find some useful math functions in PHP
Trigonometric Functions
These functions are use to calculate angles and lengths, using the 3 base functions sin, cosine, and tangent
- The sin function is used to calculate the sine of an angle in radians
float sin(float $angle)
Parameters:
- float $angle: The angle in radians.
Return Values:
- This returns the sine of $angle
Examples:
<?php
$angle=3*pi()/2;
echo sin($angle);
//Returns:
-1
?>
- cos takes an angle in radians and returns the cosine of it
float cos(float $angle)
Parameters:
- float $angle: The angle in radians.
Return Values:
- This returns the cosine of $angle
Examples:
<?php
$angle=3*pi()/2;
echo cos($angle); //Returns a very low number because of floating point errors. Deal with it
//Returns:
-1.836970198721E-16
?>
- tan takes an angle in radians and returns the tangent of it
float tan(float $angle)
Parameters:
- float $angle: The angle in radians.
Return Values:
- This returns the tangent of $angle
Examples:
<?php
$angle=3*pi()/2;
echo tan($angle); //Returns a very high number, not undefined or NaN because of floating point errors. Deal with it
//Returns:
5.4437464510651E+15
?>
- asin takes a value between -1 and 1, and returns the arcsine of it
float asin(float $val)
Parameters:
- float $val: A number between 1 and 1
Return Values:
- Returns an angle in radians
Examples:
<?php
$val=sin(3*pi()/2);
echo asin($val); // -π/2==3π/2
//Returns:
-1.5707963267949
?>
- ascos takes a value between -1 and 1, and returns the arcsine of it
float acos(float $val)
Parameters:
- float $val: A number between 1 and 1
Return Values:
- Returns an angle in radians
Examples:
<?php
$value=cos(3*pi()/2);
echo acos($value);
//Returns:
1.5707963267949
?>
- atan takes a value and returns the arctangent of it
float atan(float $val)
Parameters:
- float $val: A number between 1 and 1
Return Values:
- Returns an angle in radians
Examples:
<?php
$value=tan(3*pi()/2);
echo atan($value);
//Returns:
1.5707963267949
?>
Base Conversions
This is used to convert numbers into different bases that the computer uses, such as binary, octal, decimal, and hexadecimal
-
<?php
$number=343789;
$oldbase=10;
$newbase=16; //Hexadecimal
$newnumber=base_convert($number,$oldbase,$newbase);
$newnewnumber=base_convert($newnumber,$newbase,$oldbase);
echo "Hexadecimal version is $newnumber and decimal version is $newnewnumber";
Hexadecimal version is 53eed and decimal version is 343789
?>
- Note that there are more specific versions of this function, to convert to special bases. Those functions are : decbin, bindec, decoct, octdec, dechex, and hexdec. You can read more about those here
<?php
$decimal=73287;
$hex=dechex($decimal);
$newdecimal=hexdec($hex);
echo "$hex<br>$newdecimal"; //No this is not 11*10^47, e is a hex character.
//Returns:
11e47
73287
?>
There are more functions like this, to convert from octal to binary to decimal, but these are less used than conversions to and from hexadecimal. You can learn more about these here