PHP Number Functions
PHP provides a wide range of number-related functions for performing calculations, formatting numbers, and handling mathematical operations. Here’s a list of important number functions in PHP:
Mathematical Functions:
abs()
- Returns the absolute value of a number.- Example:
abs(-4.5)
returns4.5
.
- Example:
ceil()
- Rounds a number up to the nearest integer.- Example:
ceil(4.3)
returns5
.
- Example:
floor()
- Rounds a number down to the nearest integer.- Example:
floor(4.7)
returns4
.
- Example:
round()
- Rounds a floating-point number to a specified precision.- Example:
round(4.567, 2)
returns4.57
.
- Example:
sqrt()
- Returns the square root of a number.- Example:
sqrt(16)
returns4
.
- Example:
pow()
- Raises a number to the power of another.- Example:
pow(2, 3)
returns8
.
- Example:
max()
- Returns the highest value in an array or a list of numbers.- Example:
max(2, 3, 9, 4)
returns9
.
- Example:
min()
- Returns the lowest value in an array or a list of numbers.- Example:
min(2, 3, 9, 4)
returns2
.
- Example:
rand()
- Generates a random integer.- Example:
rand(1, 100)
returns a random integer between 1 and 100.
- Example:
mt_rand()
- Generates a better random number (Mersenne Twister algorithm).- Example:
mt_rand(1, 100)
.
- Example:
number_format()
- Formats a number with grouped thousands.- Example:
number_format(1234567.89, 2)
returns1,234,567.89
.
- Example:
is_nan()
- Checks whether a value is not a number (NaN).- Example:
is_nan(NAN)
returnstrue
.
- Example:
fmod()
- Returns the floating-point remainder of division.- Example:
fmod(5.7, 1.3)
returns0.5
.
- Example:
intdiv()
- Performs integer division.- Example:
intdiv(10, 3)
returns3
.
- Example:
log()
- Returns the natural logarithm of a number (or logarithm to a specified base).- Example:
log(8, 2)
returns3
.
- Example:
exp()
- Returns e raised to the power of a number (exponential function).- Example:
exp(1)
returns approximately2.71828
.
- Example:
decbin()
- Converts a decimal number to binary.- Example:
decbin(10)
returns1010
.
- Example:
dechex()
- Converts a decimal number to hexadecimal.- Example:
dechex(255)
returnsff
.
- Example:
decoct()
- Converts a decimal number to octal.- Example:
decoct(10)
returns12
.
- Example:
bindec()
- Converts a binary number to decimal.- Example:
bindec('1010')
returns10
.
- Example:
hexdec()
- Converts a hexadecimal number to decimal.- Example:
hexdec('ff')
returns255
.
- Example:
octdec()
- Converts an octal number to decimal.- Example:
octdec('12')
returns10
.
- Example:
pi()
- Returns the value of π (pi).- Example:
pi()
returns3.1415926535898
.
- Example:
is_finite()
- Checks if a value is a finite number.- Example:
is_finite(3.4)
returnstrue
.
- Example:
is_infinite()
- Checks if a value is an infinite number.- Example:
is_infinite(INF)
returnstrue
.
- Example:
is_numeric()
- Checks whether a variable is a number or a numeric string.- Example:
is_numeric("123")
returnstrue
.
- Example:
hypot()
- Returns the length of the hypotenuse of a right-angle triangle.- Example:
hypot(3, 4)
returns5
.
- Example:
deg2rad()
- Converts a number from degrees to radians.- Example:
deg2rad(180)
returns3.14159
.
- Example:
rad2deg()
- Converts a number from radians to degrees.- Example:
rad2deg(pi())
returns180
.
- Example:
sin()
- Returns the sine of a number (angle in radians).- Example:
sin(pi()/2)
returns1
.
- Example:
cos()
- Returns the cosine of a number (angle in radians).- Example:
cos(pi())
returns-1
.
- Example:
tan()
- Returns the tangent of a number (angle in radians).- Example:
tan(pi()/4)
returns1
.
- Example:
asin()
- Returns the arcsine of a number (result in radians).- Example:
asin(1)
returnspi()/2
.
- Example:
acos()
- Returns the arccosine of a number (result in radians).- Example:
acos(1)
returns0
.
- Example:
atan()
- Returns the arctangent of a number (result in radians).- Example:
atan(1)
returnspi()/4
.
- Example:
atan2()
- Returns the angle (in radians) from the X-axis to a point (Y, X).- Example:
atan2(1, 1)
returnspi()/4
.
- Example:
base_convert()
- Converts a number between arbitrary bases.- Example:
base_convert('A37334', 16, 2)
converts from hexadecimal to binary.
- Example:
These functions allow PHP to handle various mathematical operations, number formatting, and number conversions, making it a powerful tool for building applications that require numeric computations.