Open In App

PHP Math Functions

Last Updated : 17 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

PHP is a scripting language that comes with many built-in math functions and constants. These tools help you do basic math, solve more complex problems, and work with different math concepts. This guide gives an overview of PHP's math functions and constants, along with examples, practical uses, and helpful tips.

Note: The PHP math functions are built into PHP, so you don't need to install anything to use them.

PHP Math Functions

PHP offers numerous mathematical functions that allow you to perform operations like rounding, trigonometry, logarithms, and more. Here are the most commonly used functions:

Math FunctionDescription
abs()Returns the absolute value of a number.
acos()Returns the arc cosine (inverse of cosine) of a number in radians.
acosh()Returns the inverse hyperbolic cosine of a number.
asin()Returns the arc sine (inverse of sine) of a number in radians.
asinh()Returns the inverse hyperbolic sine of a number.
atan()Returns the arc tangent (inverse of tangent) of a number in radians.
atan2()Returns the arc tangent of the two variables in radians.
atanh()Returns the inverse hyperbolic tangent of a number.
base_convert()Converts a number between different number bases.
bindec()Converts a binary number to decimal.
ceil()Rounds a number up to the nearest integer.
cos()Returns the cosine of an angle in radians.
cosh()Returns the hyperbolic cosine of an angle in radians.
decbin()Converts a decimal number to a binary number.
dechex()Converts a decimal number to a hexadecimal number.
decoct()Converts a decimal number to an octal number.
deg2rad()Converts an angle from degrees to radians.
exp()Returns e raised to the power of a given number.
expm1()Returns e raised to the power of a number minus 1.
floor()Rounds a number down to the nearest integer.
fmod()Returns the remainder of the division of two numbers.
getrandmax()Returns the largest possible random value returned by rand().
hexdec()Converts a hexadecimal number to a decimal number.
hypot()Returns the square root of the sum of squares of its arguments.
intdiv()Performs integer division (returns the quotient of two numbers).
is_finite()Checks whether a number is finite.
is_infinite()Checks whether a number is infinite.
is_nan()Checks whether a value is NaN (Not a Number).
lcg_value()Returns a pseudo-random number in the range between 0 and 1.
log()Returns the natural logarithm (base e) of a number.
log10()Returns the base-10 logarithm of a number.
log1p()Returns the logarithm of 1 + number.
max()Returns the highest value from a given set of numbers.
min()Returns the lowest value from a given set of numbers.
mt_getrandmax()Returns the largest possible value returned by mt_rand().
mt_rand()Generates a random number using the Mersenne Twister algorithm.
mt_srand()Seeds the Mersenne Twister random number generator.
octdec()Converts an octal number to a decimal number.
pi()Returns the value of Pi (Ï€).
pow()Returns the result of raising a number to the power of another number.
rad2deg()Converts an angle from radians to degrees.
rand()Generates a random integer.
round()Rounds a floating-point number to the nearest integer.
sin()Returns the sine of an angle in radians.
sinh()Returns the hyperbolic sine of an angle in radians.
sqrt()Returns the square root of a number.
srand()Seeds the random number generator.
tan()Returns the tangent of an angle in radians.
tanh()Returns the hyperbolic tangent of an angle in radians.

PHP Predefined Math Constants

PHP provides several predefined constants for mathematical operations. These constants can be used directly in your calculations without the need to define them.

ConstantValueDescription
INFINFRepresents infinity.
M_E2.7182818284590The mathematical constant e (Euler's number).
M_EULER0.5772156649015The Euler's constant, often used in various mathematical formulas.
M_LNPI1.1447298858494The natural logarithm of Pi (log_e(pi)).
M_LN20.6931471805599The natural logarithm of 2 (log_e(2)).
M_LN102.3025850929940The natural logarithm of 10 (log_e(10)).
M_LOG2E1.4426950408890The logarithm of e with base 2 (log_2(e)).
M_LOG10E0.4342944819033The logarithm of e with base 10 (log_10(e)).
M_PI3.1415926535898The value of Pi (Ï€), the ratio of a circle's circumference to its diameter.
M_PI_21.5707963267949Pi divided by 2, used in trigonometry for 90 degrees (Ï€/2).
M_PI_40.7853981633974Pi divided by 4, used in trigonometry for 45 degrees (Ï€/4).
M_1_PI0.3183098861830The reciprocal of Pi (1/Ï€).
M_2_PI0.6366197723670The reciprocal of 2 * Pi (2/Ï€).
M_SQRTPI1.7724538509055The square root of Pi (√π).
M_2_SQRTPI1.1283791670955Two times the reciprocal of the square root of Pi (2/√π).
M_SQRT1_20.7071067811865The square root of 1/2 (√(1/2)), also equal to 1/√2.
M_SQRT21.4142135623731The square root of 2 (√2).
M_SQRT31.7320508075689The square root of 3 (√3).
NANNANRepresents a value that is "Not A Number", usually due to undefined mathematical operations.
PHP_ROUND_HALF_UP1Rounds a number up when it is halfway between two integers.
PHP_ROUND_HALF_DOWN2Rounds a number down when it is halfway between two integers.
PHP_ROUND_HALF_EVEN3Rounds a number to the nearest even integer when it is halfway between two integers.
PHP_ROUND_HALF_ODD4Rounds a number to the nearest odd integer when it is halfway between two integers.

Best Practices for Using PHP Math Functions

  • Use Constants for Accuracy: When working with numbers like Pi or Euler's number, it's better to use the predefined constants like M_PI and M_E instead of typing the values yourself. These constants are more accurate.
  • Avoid Repetitive Calculations: When you need to perform complex calculations multiple times, consider storing results in variables instead of recalculating the same result multiple times.
  • Ensure Correct Input: Many mathematical functions, such as sqrt(), may not work correctly with negative numbers. Always validate your input to avoid errors.
  • Use Random Number Functions Carefully: When generating random numbers, prefer using mt_rand() or random_int() for better performance and security, especially when dealing with sensitive data.

Conclusion

PHP offers a wide range of math functions that make it easier for developers to perform tasks like basic math, trigonometry, and logarithms. These built-in tools help simplify coding, ensuring your calculations are accurate and efficient.


Next Article
Article Tags :
Practice Tags :

Similar Reads