Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page