Performing Math Operations

PHP has many built-in functions that help you do anything from simple addition or subtraction to advanced computation.
Example:
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP Basic Math Operations</title> | |
</head> | |
<body> | |
<?php | |
echo 7 + 3 . "<br>"; // 0utputs: 10 | |
echo 7 - 2 . "<br>"; // 0utputs: 5 | |
echo 7 * 2 . "<br>"; // 0utputs: 14 | |
echo 7 / 2 . "<br>"; // 0utputs: 3.5 | |
echo 7 % 2 . "<br>"; // 0utputs: 1 | |
?> | |
</body> | |
</html> |
Each math operation has a certain precedence level; Multiplication and division are usually done before addition and subtraction. However, parentheses can change this precedence; Sentences enclosed within parentheses are always evaluated, regardless of prior level of operation, as in the following example.
Example:
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP Change Operator Precedence Using Parentheses</title> | |
</head> | |
<body> | |
<?php | |
echo 5 + 4 * 10 . "<br>"; // 0utputs: 45 | |
echo (5 + 4) * 10 . "<br>"; // 0utputs: 90 | |
echo 5 + 4 * 10 / 2 . "<br>"; // 0utputs: 25 | |
echo 8 * 10 / 4 - 2 . "<br>"; // 0utputs: 18 | |
echo 8 * 10 / (4 - 2) . "<br>"; // 0utputs: 40 | |
echo 8 + 10 / 4 - 2 . "<br>"; // 0utputs: 8.5 | |
echo (8 + 10) / (4 - 2) . "<br>"; // 0utputs: 9 | |
?> | |
</body> | |
</html> |
In the following section we’re going to look at some built-in PHP functions that are most frequently used in performing mathematical operations.
Find the Absolute Value of a Number
The absolute value of an integer or float can be found with the abs () function, as shown in the following Example:
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP Get Absolute Value of a Number</title> | |
</head> | |
<body> | |
<?php | |
echo abs(5) . "<br>"; // 0utputs: 5 (integer) | |
echo abs(-5) . "<br>"; // 0utputs: 5 (integer) | |
echo abs(4.2) . "<br>"; // 0utputs: 4.2 (double/float) | |
echo abs(-4.2) . "<br>"; // 0utputs: 4.2 (double/float) | |
?> | |
</body> | |
</html> |
As you can see if the given number is negative or not, the valued return is positive. But, if the number is positive, this function only returns the number.
Round a Fractional Value Up or Down
The ceiling () function can be used to round a fractional value to the next highest integer value, while the floor () function can be used to round a fractional value to the next lowest integer value, As shown in the following Example:
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP Get Ceil and Floor Value of a Number</title> | |
</head> | |
<body> | |
<?php | |
// Round fractions up | |
echo ceil(4.2) . "<br>"; // 0utputs: 5 | |
echo ceil(9.99) . "<br>"; // 0utputs: 10 | |
echo ceil(-5.18) . "<br>"; // 0utputs: -5 | |
// Round fractions down | |
echo floor(4.2) . "<br>"; // 0utputs: 4 | |
echo floor(9.99) . "<br>"; // 0utputs: 9 | |
echo floor(-5.18) . "<br>"; // 0utputs: -6 | |
?> | |
</body> | |
</html> |
Find the Square Root of a Number
You can use the sqrt () function to find the square root of a positive number. This function returns a special value NAN for negative numbers.
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP Get Square Root of a Number</title> | |
</head> | |
<body> | |
<?php | |
echo sqrt(9) . "<br>"; // 0utputs: 3 | |
echo sqrt(25) . "<br>"; // 0utputs: 5 | |
echo sqrt(10) . "<br>"; // 0utputs: 3.1622776601684 | |
echo sqrt(-16) . "<br>"; // 0utputs: NAN | |
?> | |
</body> | |
</html> |
Generate a Random Number
The rand () function can be used to generate a random number. You can optionally specify a range by passing minimum, maximum arguments,
Example
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP Generate a Random Number</title> | |
</head> | |
<body> | |
<?php | |
// Generate some random numbers | |
echo rand() . "<br>"; | |
echo rand() . "<br>"; | |
// Generate some random numbers between 1 and 120 (inclusive) | |
echo rand(1, 120) . "<br>"; | |
echo rand(1, 120) . "<br>"; | |
?> | |
</body> | |
</html> |
If the rand () function is called without optional minimum, maximum arguments, it returns a pseudo dimensional number between 0 and getrandmax (). The getrandmax () function shows the largest possible random value, which is only 32767 on the Windows platform. Therefore, if you need a range larger than 32767, you can specify only minimum and maximum arguments.




I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND