Floats

Integers are good for whole numbers, but for everything else you will need floating-point numbers, often called real numbers or just floats. These are numbers like 1.1, 1.1111112, -12345678.9123, and even 1.0. You may also specify an exponent with your float, i.e., 3.14159e4 is equal to 31415.9.

You may not specify your floats using anything but decimal, so -0x4.AF will generate an error. Unlike Perl, there is no thousands separator in PHP, so values such as 1_221_279 will not work.

Here are some examples of floating-point arithmetic:

    $a = 1.132324;
    $b = $a + 1;
    $b = $a + 1.0;
    $c = 1.1e15;
    $d = (0.1+0.7) * 10;

Mixing a float with an integer, as in line two, results in another float so that PHP doesn't lose any accuracy. Line four specifies a very large exponent; if you print out the resulting number, you will actually get 1.1E+015 back because the number is so large.

The last example appears to assign the value 8 to $d, but owing to inherent inconsistencies in floating-point numbers, the value will actually be 7.9999999999999991. Usually this is not a problem, because rounding that value even to 10 decimal places gives you 8, but it does mean that you should avoid comparing floating-point numbers if possible.

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.