Tutorials – Other Maths Operators in PHP

maths symbols 2

maths symbols 2

 

Subtraction in PHP

We’re not going to weigh things down by subjecting you to torrents of heavy Math! But you do need to know how to use the basic operators. First up is subtracting.

 

 

 

 

To add up using PHP variables, you did this:

<?php

$first_number = 10;
$second_number = 20;
$sum_total = $first_number + $second_number;

print ($sum_total);

?>

Subtraction is more or less the same. Instead of the plus sign (+), simply use the minus sign (-). Change your $sum_total line to this, and run your code:

$sum_total = $second_number – $first_number;

The s$sum_total line is more or less the same as the first one. Except we’re now using the minus sign instead (and reversing the two variables). When you run the script you should, of course, get the answer 10. Again, PHP knows what is inside of the variables called $second_number and $first_number. It knows this because you assigned values to these variables in the first two lines. When PHP comes across the minus sign, it does the subtraction for you, and puts the answer into the variable on the left of the equals sign. We then use a print statement to display what is inside of the variable.

Just like addition, you can subtract more than one number at a time. Try this:

<?php

$first_number = 10;
$second_number = 20;
$third_number = 100;

$sum_total = $third_number – $second_number – $first_number;

print ($sum_total);

?>

The answer you should get is 70. You can also mix addition with subtraction. Here’s an example:

<?php

$first_number = 10;
$second_number = 20;
$third_number = 100;

$sum_total = $third_number – $second_number + $first_number;

print ($sum_total);

?>

Run the code above. What answer did you get? Was it the answer you were expecting? Why do you think it printed the number it did? If you thought it might have printed a different answer to the one you got, the reason might be the way we set out the sum. Did we mean 100 – 20, and then add the 10? Or did we mean add up 10 and 20, then take it away from 100? The first sum would get 90, but the second sum would get 70.

To clarify what you mean, you can use parentheses in your sums. Here’s the two different versions of the sum. Try them both in your code. But note where the parentheses are:

Version one
$sum_total = ($third_number – $second_number) + $first_number;

Version two
$sum_total = $third_number – ($second_number + $first_number);

It’s always a good idea to use parentheses in your sums, just to clarify what you want PHP to calculate. That way, you won’t get a peculiar answer!

Another reason to use parentheses is because of something called operator precedence. In PHP, some operators (Math symbols) are calculated before others. This means that you’ll get answers that are entirely unexpected!

 

maths symbols 2

 

Multiplication in PHP

To multiply in PHP (and just about every other programming language), the * symbol is used. If you see 20 * 10, it means multiply 20 by 10. Here’s some code for you to try:

<?php

$first_number = 10;
$second_number = 20;
$sum_total = $second_number * $first_number;

print ($sum_total);

?>

In the above code, we’re just multiplying whatever is inside of our two variables. We’re then assigning the answer to the variable on the left of the equals sign. (You can probably guess what the answer is without running the code!)

Just like addition and subtraction, you can multiply more than two numbers:

<?php

$first_number = 10;
$second_number = 20;
$third_number = 100;

$sum_total = $third_number * $second_number * $first_number;

print ($sum_total);

?>

And you can even do this:

$sum_total = $third_number * $second_number * 10;

But try this code. See if you can guess what the answer is before trying it out:

<?php

$first_number = 10;
$second_number = 2;
$third_number = 3;

$sum_total = $third_number + $second_number * $first_number;

print ($sum_total);

?>

What answer did you expect? If you were expecting to get an answer of 50 then you really need to know about operator precedence! As was mentioned, some operators (Math symbols) are calculated before others in PHP. Multiplication and division are thought to be more important that addition and division. So these will get calculated first. In our sum above, PHP sees the * symbol, and then multiplies these two numbers first. When it works out the answer, it will move on to the other symbol, the plus sign. It does this first:

$second_number * $first_number;

Then it moves on to the addition. It doesn’t do this first:

$third_number + $second_number

This makes the parentheses more important than ever! Use them to force PHP to work out the sums your way. Here’s the two different version. Try them both:

Version one
$sum_total = $third_number + ($second_number * $first_number);

Version two
$sum_total = ($third_number + $second_number) * $first_number;

Here’s we’re using parentheses to force two different answers. PHP will work out the sum between the parentheses first, and then move on to the other operator. In version one, we’re using parentheses to make sure that PHP does the multiplication first. When it gets the answer to the multiplication, THEN the addition is done. In version two, we’re using parentheses to make sure that PHP does the addition first. When it gets the answer to the addition, THEN the multiplication is done.

 

maths symbols 2

 

Division in PHP

To divide one number by another, the / symbol is used in PHP. If you see 20 / 10, it means divide 10 into 20. Try it yourself:

<?php

$first_number = 10;
$second_number = 20;
$sum_total = $second_number / $first_number;

print ($sum_total);

?>

Again, you have to be careful of operator precedence. Try this code:

<?php

$first_number = 10;
$second_number = 20;
$third_number = 100;

$sum_total = $third_number – $second_number / $first_number;

print ($sum_total);

?>

PHP won’t work out the sum from left to right! Division is done before subtraction. So this will get done first:

$second_number / $first_number

And NOT this:

$third_number – $second_number

Using parentheses will clear things up. Here’s the two versions for you to try:

Version one
$sum_total = $third_number – ($second_number / $first_number);

Version two
$sum_total = ($third_number – $second_number) / $first_number;

The first version will get you an answer of 98, but the second version gets you an answer of 8! So remember this: division and multiplication get done BEFORE subtraction and addition. Use parentheses if you want to force PHP to calculate a different way.
Download the PHP Course Files

From http://www.homeandlearn.co.uk