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

  • pi

    • pi returns the value of pi
    • float pi()
    • Parameters: None
      Return Values: Returns pi
  • deg2rad and rad2deg

    • deg2rad takes a angle in degrees and converts it to radians, and rad2deg does the opposite
    • float rad2deg(float $radians)
    • float deg2rad(float $degrees)
    • Parameters:
      • deg2rad takes a floating point number of degrees
      • rad2deg takes a floating point number of radians
      Return Values:
      • rad2deg returns a floating point number of degrees
      • deg2rad returns a floating point number of radians
      Examples:
      <?php
      	$degree=270;
      	$rad=deg2rad($degree);
      	$newdegree=rad2deg($rad);
      	echo "$rad<br>$newdegree";
      	
      	4.7123889803847
      270 ?>
  • sin

    • 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

    • 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

    • 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

    • 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									
      
      ?>
  • acos

    • 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

    • 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

  • base_convert

    • base_convert takes a number, and the old base that it is in, and the base you want to convert it to, and converts it. The bases can be between base 2 and base 36
    • string base_convert ( string $number , int $oldbase , int $newbase)
    • Parameters:
      • string $number: The number that you want to change the base of
      • int $oldbase: The base that $number is in
      • int $newbase: The base that you want $number to be in
      Return Values:
      • This function always returns a string with the new number
      Examples:
      <?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
  • hexdec and dechex

    • hexdec takes a string in hexadecimal, and returns a number in decimal. dechex does the opposite. It takes a number in decimal, and returns a hex string
    • float hexdec(string $hex)
    • string dechex(float $decimal)
    • Parameters:
      • hexdec takes a string in hex, such as "#F2J82A"
      • dechex takes a number in decimal, such as 42.
      Return Values:
      • hexdec returns a number in decimal
      • dechex returns a string in hexadecimal
      Examples:
      <?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