PHP Math

PHP has several built-in functions for performing mathematical operations, such as basic arithmetic, rounding, and generating random numbers.

Here are some examples of common math functions:

  • abs() – Returns the absolute value of a number.
  • round() – Rounds a number to the nearest integer or to a specified number of decimal places.
  • ceil() – Rounds a number up to the nearest integer.
  • floor() – Rounds a number down to the nearest integer.
  • max() – Returns the highest value in a list of numbers.
  • min() – Returns the lowest value in a list of numbers.
  • mt_rand() – Generates a random integer.
  • rand() – Generates a random integer.

Here are some examples of how you can use these functions:

echo abs(-123); // Outputs 123
echo round(3.14159, 2); // Outputs 3.14
echo ceil(3.14159); // Outputs 4
echo floor(3.14159); // Outputs 3
echo max(1, 2, 3, 4, 5); // Outputs 5
echo min(1, 2, 3, 4, 5); // Outputs 1
echo mt_rand(1, 10); // Outputs a random integer between 1 and 10 (inclusive)
echo rand(1, 10); // Outputs a random integer between 1 and 10 (inclusive)