Cover Image for PHP Math Functions
192 views

PHP Math Functions

PHP provides a variety of built-in math functions that allow you to perform mathematical operations and calculations. Here are some commonly used PHP math functions:

  1. abs(): Returns the absolute value of a number.
$number = -10;
echo abs($number); // Output: 10
  1. round(): Rounds a floating-point number to the nearest integer.
$number = 3.6;
echo round($number); // Output: 4
  1. ceil(): Rounds a floating-point number up to the nearest integer.
$number = 3.1;
echo ceil($number); // Output: 4
  1. floor(): Rounds a floating-point number down to the nearest integer.
$number = 3.9;
echo floor($number); // Output: 3
  1. rand(): Generates a random integer between a given range.
$randomNumber = rand(1, 100);
echo $randomNumber; // Output: Random number between 1 and 100
  1. min() and max(): Returns the minimum and maximum values from a list of arguments.
$numbers = [5, 10, 3, 8];
echo min($numbers); // Output: 3
echo max($numbers); // Output: 10
  1. sqrt(): Calculates the square root of a number.
$number = 25;
echo sqrt($number); // Output: 5
  1. pow(): Raises a number to a power.
$base = 2;
$exponent = 3;
echo pow($base, $exponent); // Output: 8 (2^3)
  1. rand(): Generates a random number between 0 and 1.
$randomNumber = rand();
echo $randomNumber; // Output: Random number between 0 and 1
  1. pi(): Returns the value of pi (π).
echo pi(); // Output: 3.1415926535898

These are just a few examples of the many useful math functions available in PHP. There are many more functions for trigonometry, logarithms, rounding, and more. Always refer to the official PHP documentation for a complete list of math functions and their usages.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS