Numbers in PHP

Numbers in PHP are of two scalar types: the integers of the first section and the floats after them. echo prints either one, and var_dump also shows the type.

Integers

Integers are whole numbers within the platform range, usually 64 bit today. Underscores may split long literals for reading:

<?php $views = 1_000_000; var_dump($views); var_dump(PHP_INT_MAX);

php ints.php

int(1000000)
int(9223372036854775807)

The underscores are for the reader only: var_dump shows the plain 1000000. The last line is the largest integer this machine holds; one more becomes a float.

Floats

Floats carry a decimal point, but binary fractions cannot store every decimal exactly. Never compare money with double equals: compare the gap instead:

<?php $sum = 0.1 + 0.2; var_dump($sum); echo round(3.14159, 2), PHP_EOL;

php floats.php

float(0.30000000000000004)
3.14

The sum is not exactly 0.3, because neither 0.1 nor 0.2 has an exact binary form. Compare the distance between the two numbers with a small limit instead:

<?php $sum = 0.1 + 0.2; var_dump($sum == 0.3); var_dump(abs($sum - 0.3) < 0.000001);

php gap.php

bool(false)
bool(true)

abs() gives the size of the difference without its sign, so the second check asks a question that has a useful answer.

Common math functions

Core helpers such as abs(), min(), max() and round() cover daily needs without any setup:

<?php echo abs(-7), PHP_EOL; echo min(3, 1, 2), PHP_EOL; echo max(3, 1, 2), PHP_EOL; echo (int) 9.99, PHP_EOL;

php math.php

7
1
3
9

The cast (int) cuts the fraction off and never rounds it up, so 9.99 becomes 9.

Random numbers

random_int() deals cryptographically strong integers, which fits dice, shuffles, and tokens. A deeper recipe lives in Generate unique random numbers with PHP.

<?php $die = random_int(1, 6); var_dump($die >= 1, $die <= 6); $token = bin2hex(random_bytes(8)); echo strlen($token), PHP_EOL;

php random.php

bool(true)
bool(true)
16

The dice value itself changes on every run, so the program checks its range instead of printing it. The token comes from random_bytes() and bin2hex(), and strlen() always reports 16, because eight random bytes become sixteen hexadecimal characters.

Exercises

  1. Print PHP_INT_MAX and then the same value plus one on the next line. What type does the second line show, and why?
  2. Change the float check to use 0.5 instead of 0.3 — a value with an exact binary form — and explain why both checks are true now.
  3. Print a random number between 1 and 100 together with its length in characters, in one echo per value.
  4. Use (int) on -9.99. Does the result round towards zero or away from it?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the underscore in 1_000_000 do in PHP?

    Answer

    It is only a separator for the reader and is ignored by the parser

    PHP 7.4 and later accept underscores inside a numeric literal and ignore them, so the value is the plain one million.

  2. Why is 0.1 + 0.2 not exactly 0.3?

    Answer

    Neither decimal has an exact binary fraction, so each is stored slightly off

    A float is a binary fraction, and 0.1 and 0.2 have no exact binary form, so the stored values and their sum are slightly off.

  3. Which function returns a cryptographically strong random integer?

    Answer

    random_int()

    random_int draws from a source fit for tokens and passwords; rand and mt_rand are seeded generators.

  4. What does (int) 9.99 give?

    Answer

    9

    A cast to int throws the fraction away; it does not round, so 9.99 becomes 9.

  5. What does this program print? <?php var_dump(PHP_INT_MAX + 1);

    Answer

    float(9.223372036854776E+18)

    An integer that no longer fits the platform range becomes a float, and var_dump shows it in the exponent form.

  6. What does this program print? <?php var_dump(0.1 + 0.2 == 0.3);

    Answer

    bool(false)

    The sum is 0.30000000000000004, which is not the stored value of 0.3, so the strict comparison of the two floats is false.

  7. What does this program print? <?php echo round(3.14159, 2);

    Answer

    3.14

    round() with a precision of 2 keeps two decimals and the third digit, 1, leaves the second one alone.

  8. What does this program print? <?php echo abs(-7), PHP_EOL, min(3, 1, 2), PHP_EOL, max(3, 1, 2);

    Answer

    7 / 1 / 3

    abs drops the sign, min returns the smallest argument (1) and max the largest (3), each on a line of its own.

  9. What does this program print? <?php echo strlen(bin2hex(random_bytes(8)));

    Answer

    16

    Eight bytes become sixteen hexadecimal characters, two per byte, and strlen counts bytes, so the answer is always 16.

  10. What does this program print? <?php var_dump((int) "12 apples");

    Answer

    int(12)

    A cast reads the number at the start of the string and stops at the first non-digit, so the result is 12 without any warning.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev