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.