🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

PHP 7 Fundamental Tutorial for Beginners – PHP Math Operations

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>
view raw Operations.php hosted with ❤ by GitHub

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>
view raw Precedence.php hosted with ❤ by GitHub

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>
view raw Number.php hosted with ❤ by GitHub

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>
view raw Number.php hosted with ❤ by GitHub

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>
view raw Number.php hosted with ❤ by GitHub

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>
view raw Random.php hosted with ❤ by GitHub

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.

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.