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.
In the next part, we'll take a look at how PHP handles floating point numbers.
Tidak ada komentar:
Posting Komentar
1. "Blog ini Do Follow, silakan post untuk mendapatkan Backlink"
2. "Anda Follow, pasti saya Followback"
3. "Kalau mau Copy-Paste artikel boleh saja, tapi sumbernya ke blog ini"
4. "Terima Kasih Lagi . . . !!!"
Komentar Anda Sangat Kami Harapkan Untuk Kemajuan Blog Ini. isikan komentar anda disini !