# Lesson 4 – PHP Mathematics

Mathematics in PHP is very similar to the way we do mathematics in real life. It has its own internal calculator which can perform all the mathematical operations including addition, subtraction, multiplication, and division.

Let’s say you want to add 66 with 44. What will you do? You will simply add them as you do in regular mathematics.

``````<?php
echo 56 + 44;
?>
``````

When you print the above code, you will get 100. It is as simple as that.

Now, if you want to subtract, multiply or divide the above numbers, you will write the following piece of code.

``````<?php
echo 56 + 44;
echo <br>
echo 56 – 44;
echo <br>
echo 56 * 44;
echo <br>
echo 56 / 44;
?>
``````

You can also do the following with maths in PHP.

``````<?php
echo 56 + 4 - 10 * 8 / 25;
?>``````

The above code will produce the following result.

Another important thing to remember is the order of operation. Let us check the following example.

``````<?php
echo 5 + 5 * 5;
?>
``````

The outcome of the above equation is 30. This is because multiplication takes precedence over addition in PHP, just like the regular mathematics. The overall order of operation is division followed by multiplication followed by addition and finally subtraction.

But if you want to add 5 + 5 before 5 * 5, you will add parenthesis around 5+5 as shown in the following example.

``````<?php
echo {5 + 5} * 5;
?>
``````

Now, the result of the equation will be 50 instead of 55 because Apache is adding (5 + 5) before 5 * 5. The same is the case with division. The division always goes first in a mathematical equation. Take a look at the example below.

The answer this time will be 6 because 5 divided by 5 is 1. And, 5 plus 1 is 6.

Finally, you can also store numbers in the variables and subsequently add the variables.

``````<?php
\$num1 = 20;
\$num2 = 33;

echo \$num1 + \$num2;
?>
``````

Here is the outcome of the above example.

In the next lecture, we will study arrays in PHP.