Numbers in PHP

Integers

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

$views = 1_000_000; var_dump($views); // int(1000000) var_dump(PHP_INT_MAX); // int(9223372036854775807) on 64 bit

Floats

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

$sum = 0.1 + 0.2; var_dump($sum); // float(0.30000000000000004) echo round(3.14159, 2); // 3.14

Common math functions

Core helpers cover daily needs without any setup:

echo abs(-7); // 7 echo min(3, 1, 2); // 1 echo max(3, 1, 2); // 3 echo (int) 9.99; // 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.

echo random_int(1, 6); // one of 1, 2, 3, 4, 5, 6 $token = bin2hex(random_bytes(8)); echo strlen($token); // int(16)

Next: Operators

Автор статьи: Arthur Isaev

Изображение баннера