Floating Point Numbers in PHP

 

A floating point number is one that has a dot in it, like 0.5 and 10.8. You don't need any special syntax to set these types of numbers up. Here's an example for you to try:

<?php

$first_number = 1.2;
$second_number = 2.5;
$sum_total = $second_number + $first_number;

print ($sum_total);

?>

You add up, subtract, divide and multiply these numbers in exactly the same way as the integers you've been using. A warning comes with floating point numbers, though: you shouldn't trust them, if you're after a really, really precise answer!

 

Some Exercises

To round up this section on number variables, here's a few exercises (In your print statements, there should be no numbers – just variable names):

Exercise
Write a script to add up the following figures: 198, 134, 76. Use a print statement to output your answer.

Exercise
Write a script to add up the following two numbers: 15, 45. Then subtract the answer from 100. Use a print statement to output your answer.

Exercise
Use variables to calculate the answer to the following sum:

(200 * 15) / 10

Use a print statement to output your answer.

 

In the next part of these PHP tutorials, we'll take a look at Conditional Logic.