PHP provides nearly fifty functions that can perform various mathematical functions. Combined with mathematical operators, they can make your homework a lot easier!
The simplest math functions are those that only require a single parameter. This parameter is usually a number, which is accepted either as a variable or a string. The ceil() function, for example, will return a number rounded up to the nearest full number.
$num = 55;
// Result Will Be: 55
$result = ceil(295.34);
// Result Will Be: 296
As you can see, the results of these functions can be echoed, stored in a variable, etc.
Some math functions accept multiple parameters. The rand() function, for example, generates a random number in between the two numbers that it is given. Multiple parameter are separated by commas.
echo rand(0, 500);
// Result Is: 275
If you refresh the page, you will see the above result change each time to a random number between 0 and 500.
Below is a list of almost twenty useful math functions, along with a brief description and required parameters.
|rand(min_number, max_number)||Returns a Random Integer|
|ceil(number)||Returns the Value of a Number Rounded Upwards to the Nearest Integer|
|floor(number)||Returns the Value of a Number Rounded Downwards to the Nearest Integer|
|abs(number)||Returns the Absolute Value of a Number|
|base_convert(number)||Converts a Number From One Base to Another|
|bindec(binary_number)||Converts a Binary Number to a Decimal Number|
|decbin(decimal_number)||Converts a Decimal Number to a Binary Number|
|dechex(decimal_number)||Converts a Decimal Number to a Hexadecimal Number|
|decoct(decimal_number)||Converts a Decimal Number to an Octal Number|
|fmod(number, divisor)||Returns the Remainder (Modulo) of the Division of the Arguments|
|hexdec(hex_number)||Converts a Hexadecimal Number to a Decimal Number|
|is_finite(number)||Returns True if a Value is a Finite Number|
|is_infinite(number)||Returns True if a Value is an Infinite Number|
|is_nan(value_to_check)||Returns True if a Value is Not a Number|
|max(number1, number2)||Returns the Number With the Highest Value of Two Specified Numbers|
|min(number1, number2)||Returns the Number With the Lowest Value of Two Specified Numbers|
|pi()||Returns the Value of PI|
|pow(x, y)||Returns the Value of "x" to the Power of "y"|
|round(number, optional)||Rounds a Number to the Nearest Integer (Number of Digits After Decimal Point Optional)|
|sqrt(number)||Returns the Square Root of a Number|
A complete list of the math functions available can be found in the official PHP manual. Each function includes examples that will help you understand how to use it.