There are many mathematical functions in PHP so that we can perform mathematical operations.

PHP Mathematical Operations (PHP Math)

In this lesson, we will explain PHP mathematical operations (PHP Math) with examples. Read and apply our lesson to find out.

PHP Math Function: pi()

The PHP pi() function returns the PI number. You can see how you use it in the example below.

echo pi();
// Çıktı: 3.1415926535898

Alternatively, we can use it by assigning it to a variable.

$pi_number = pi();
echo $pi_number;
// Çıktı: 3.1415926535898

PHP min() and max() Functions

The PHP min() function finds the lowest value in the given arguments, and the PHP max() function finds the highest value.

echo min(1, 83, 593, 33, 10, 493);
// Çıktı: 1

echo max(1, 83, 593, 33, 10, 493);
// Çıktı: 593

PHP abs() Function

PHP abs() function returns the absolute value of the number.

echo abs(-10);
// Çıktı: 10

PHP sqrt() Function

PHP sqrt() function returns the square root of a number.

echo sqrt(81);
// Çıktı: 9

PHP round() Function

The PHP round() function rounds a decimal number to an integer. If the second argument is not given to the function, it rounds so that there are no decimal places. If 1 is given as the second argument, it rounds to 1 decimal; if 2 is given, it rounds to 2 decimals.

echo round(1.60);
// Çıktı: 2

echo round(1.2);
// Çıktı: 1

echo round(1.68);
// Çıktı: 2

echo round(1.68,1);
// Çıktı: 1.7

echo round(1.688,2);
// Çıktı: 1.69

PHP rand() Function

The PHP rand() function generates a random number. By adding min and max parameters, lower and upper limits can be set for the random number to be generated.

echo rand();
// Çıktı: (bilinemeyen ve her seferinde değişen rastgele bir sayı)

echo rand(10,100);
// Çıktı: (bilinemeyen ve her seferinde değişen, 10 ve 100 arasında (10 ve 100 dahil) rastgele bir sayı)