PHP Math

PHP Math lets you perform mathematical operations with PHP.

Math Function Example Description  
sqrt(float $num): float echo(sqrt(25));  //outputs: 5 This math function returns the square root of a number.   Do It Yourself
pi(): float echo(pi()); //outputs: 3.1415926535898 This math function returns the value of PI.   Do It Yourself
abs(mixed $param): int|float echo(abs(-5.1)); //outputs: 5.1 This math function returns the positive value of a number.   Do It Yourself
min(mixed $param1, mixed $param2, ...mixed $params): mixed echo(min(1,4,6,7,8)); //outputs: 1 This math function returns the lowest number in a given number of arguments.   Do It Yourself
max(mixed $param1, mixed $param2, ...mixed $params): mixed echo(max(1,4,6,7,8)); //outputs: 8 This math function returns the highest number in a given number of arguments.   Do It Yourself
round(float $param, int precision=0, int mode=PHP_ROUND_HALF_UP): float echo(round(2.5)); //outputs: 3 This math function returns a rounded value to a specified precision. Precision can either be negative or zero (default).   Do It Yourself
rand(int $min, int $max): int echo(rand(0, 5)); //outputs: 4 (random number) This math function returns a pseudo-random function between the $min and $max values   Do It Yourself
floor(float $param): float echo(floor(2.5)); //outputs: 2 This math function returns the next lowest integer(as float) value by rounding it down.   Do It Yourself
pow(int|float $base, int|float $exp): int|float echo(pow(2, 3)); //outputs: 8 This math function returns the result of the $base raised to the power of $exp.   Do It Yourself
ceil(float $param): float echo(ceil(2.5));  //outputs: 3 This math function returns the next highest integer value by rounding it up.   Do It Yourself

What You Should Know at The End of This Lesson

  • You know about the PHP math functions.
  • You should know how to perform math operations with the PHP math functions.