PHP offers a variety of functions to perform mathematical operations on numbers.
PHP pi()
Function
The pi()
function returns the value of Pi:
echo(pi());
PHP min()
and max()
Functions
The min()
and max()
functions can be used to find the smallest or largest value in a set of arguments:
echo(min(10, 150, 30, 20, -8, -200)); // Outputs: -200 echo(max(10, 150, 30, 20, -8, -200)); // Outputs: 150
PHP abs()
Function
The abs()
function returns the absolute (positive) value of a number:
echo(abs(-9.7)); // Outputs: 9.7
PHP sqrt()
Function
The sqrt()
function returns the square root of a number:
echo(sqrt(81)); // Outputs: 9
PHP round()
Function
The round()
function rounds a floating-point number to the nearest integer:
echo(round(0.70)); // Outputs: 1 echo(round(0.49)); // Outputs: 0
Random Numbers
The rand()
function generates a random number:
echo(rand());
To generate a random number within a specific range, you can include optional minimum and maximum parameters. For example, to get a random integer between 50 and 150:
echo(rand(50, 150));