|
Free
PHP Tutorials
|
![]() |
home |
Stay
at Home and Learn
|
|||||
PHP Tutorials |
||||||
|
PHP and Division
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; print ($sum_total); ?> Again, you have to be careful of operator precedence. Try this code: <?php $first_number = 10; $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 Version two 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.
In the next part, we'll take a look at how PHP handles floating point numbers. |