php_ref_math
PHP Math Functions and Constants
Section titled “PHP Math Functions and Constants”PHP Math Introduction
Section titled “PHP Math Introduction”PHP provides a comprehensive set of functions for performing mathematical operations, ranging from basic arithmetic and rounding to trigonometric and logarithmic calculations.
These functions operate on PHP’s standard numeric types: int (integers) and float (floating-point numbers, also known as doubles).
Installation
Section titled “Installation”The core math functions and constants are built into PHP. No separate installation or extension enabling is required to use them.
Common PHP Math Functions
Section titled “Common PHP Math Functions”| Function | Description |
|---|---|
abs() | Returns the absolute (non-negative) value of a number. |
ceil() | Rounds a number up to the nearest integer. |
floor() | Rounds a number down to the nearest integer. |
round() | Rounds a floating-point number to a specified number of decimal places (or to the nearest integer if precision is omitted). Supports different rounding modes (e.g., PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN). |
min() | Returns the lowest value from a list of arguments or an array. |
max() | Returns the highest value from a list of arguments or an array. |
rand() / mt_rand() | Generates a pseudo-random integer. mt_rand() (Mersenne Twister) is generally preferred for better randomness and performance. |
srand() / mt_srand() | Seeds the random number generator. Usually not needed unless you require a specific repeatable sequence. |
getrandmax() / mt_getrandmax() | Returns the maximum possible value returned by rand() or mt_rand() respectively. |
sqrt() | Returns the square root of a number. |
pow() | Returns a base raised to the power of an exponent (e.g., pow(2, 8) is 2^8). |
exp() | Calculates the exponent of e (Euler’s number). |
log() | Returns the natural logarithm (base e) of a number. |
log10() | Returns the base-10 logarithm of a number. |
pi() | Returns the approximate value of Pi (π). |
is_nan() | Checks if a value is ‘Not A Number’ (often resulting from invalid calculations). |
is_finite() / is_infinite() | Checks if a value is finite or infinite. |
intdiv() | (PHP 7+) Performs integer division, returning only the integer part of the quotient. |
fmod() | Returns the floating-point remainder (modulo) of dividing two numbers. |
base_convert() | Converts a number between arbitrary bases (e.g., binary, octal, decimal, hexadecimal). |
bindec(), octdec(), hexdec() | Convert binary, octal, or hexadecimal strings to decimal integers. |
decbin(), decoct(), dechex() | Convert decimal integers to binary, octal, or hexadecimal strings. |
Trigonometric Functions:
Section titled “Trigonometric Functions:”PHP includes standard trigonometric functions like sin(), cos(), tan(), asin(), acos(), atan(), atan2(), as well as their hyperbolic counterparts (sinh(), cosh(), tanh(), etc.). It also provides functions for degree/radian conversion (deg2rad(), rad2deg()). Note that these functions typically work with radians.
Predefined Math Constants
Section titled “Predefined Math Constants”PHP provides several predefined constants for mathematical values.
| Constant | Description |
|---|---|
M_PI | Value of Pi (≈ 3.14159) |
M_E | Value of Euler’s number e (≈ 2.71828) |
M_PI_2 | Pi divided by 2 |
M_PI_4 | Pi divided by 4 |
M_1_PI | 1 divided by Pi |
M_2_PI | 2 divided by Pi |
M_SQRTPI | Square root of Pi |
M_2_SQRTPI | 2 divided by the square root of Pi |
M_LN2 | Natural logarithm of 2 |
M_LN10 | Natural logarithm of 10 |
M_LOG2E | Base-2 logarithm of e |
M_LOG10E | Base-10 logarithm of e |
M_SQRT2 | Square root of 2 |
M_SQRT3 | Square root of 3 |
M_SQRT1_2 | Square root of 1/2 |
INF | Represents infinity (floating-point value) |
NAN | Represents ‘Not A Number’ (floating-point value) |
PHP_INT_MAX | The largest integer supported in the current PHP build. |
PHP_INT_MIN | The smallest integer supported in the current PHP build. |
PHP_INT_SIZE | The size of an integer in bytes. |
PHP_FLOAT_DIG | Number of decimal digits that can be rounded into a float and back without precision loss. |
PHP_FLOAT_EPSILON | Smallest representable positive number x, so that x + 1.0 != 1.0. |
PHP_FLOAT_MIN | Smallest representable positive floating point number. |
PHP_FLOAT_MAX | Largest representable floating point number. |
PHP_ROUND_HALF_UP | Rounding mode constant for round(): Rounds halves away from zero. |
PHP_ROUND_HALF_DOWN | Rounding mode constant for round(): Rounds halves towards zero. |
PHP_ROUND_HALF_EVEN | Rounding mode constant for round(): Rounds halves towards the nearest even value. |
PHP_ROUND_HALF_ODD | Rounding mode constant for round(): Rounds halves towards the nearest odd value. |
For a complete and up-to-date list, always refer to the official PHP documentation.