TExcelTAD
Home › PHP › PHP Numbers

PHP Numbers

PHP has two numeric types: int for whole numbers and float for numbers with a fractional part. It also has a handy family of functions for checking whether a value is numeric, which matters a lot because data from forms always arrives as strings.

Integers

An integer is a whole number without a decimal point. On today's 64-bit systems it ranges from about −9.2 quintillion to +9.2 quintillion. You can write integers in several notations, and underscores may be used as visual separators (PHP 7.4+):

php · try it yourself
<?php
$decimal = 255;
$hex = 0xFF;
$octal = 0o377;       // PHP 8.1+ explicit octal prefix
$binary = 0b11111111;
$million = 1_000_000;

var_dump($decimal, $hex, $octal, $binary, $million);
echo PHP_INT_MAX, "\n";
echo PHP_INT_SIZE, " bytes\n";
int(255)
int(255)
int(255)
int(255)
int(1000000)
9223372036854775807
8 bytes

If a calculation goes beyond PHP_INT_MAX, PHP silently switches to a float instead of wrapping around:

php · try it yourself
<?php
$big = PHP_INT_MAX;
var_dump($big + 1);
var_dump(is_int($big), is_int($big + 1));
float(9.223372036854776E+18)
bool(true)
bool(false)

Floats

A float (also called double) holds numbers like 3.14, -0.5 or 2.5e3 (2.5 × 10³). Floats are stored in binary, so most decimal fractions are only approximations. That is why 0.1 + 0.2 is not exactly 0.3:

php · try it yourself
<?php
$sum = 0.1 + 0.2;

echo $sum, "\n";            // echo rounds for display
var_dump($sum);             // var_dump shows the full value
var_dump($sum == 0.3);
var_dump(abs($sum - 0.3) < PHP_FLOAT_EPSILON);

var_dump(2.5e3, 10 / 4, 10 / 5);
0.3
float(0.30000000000000004)
bool(false)
bool(true)
float(2500)
float(2.5)
int(2)

Tip: Never compare floats with ==. Compare the difference against a small tolerance, or round both sides first. For money, store amounts in cents as integers.

Notice the last line: / returns an int when the division is exact and a float otherwise. Use intdiv(10, 4) when you want integer division (2).

Special float values: infinity and NaN

Constant / function Meaning
PHP_FLOAT_MAX largest float (≈ 1.8e308)
INF / is_infinite() a value too large to represent
NAN / is_nan() "not a number", e.g. sqrt(-1)
is_finite() true for normal numbers
php · try it yourself
<?php
$huge = PHP_FLOAT_MAX * 2;
var_dump($huge, is_infinite($huge));

$bad = sqrt(-1);
var_dump(is_nan($bad));
var_dump(is_finite(42.0));
float(INF)
bool(true)
bool(true)
bool(true)

Numeric strings

Input from forms, URLs and files is text. is_numeric() tells you whether a string looks like a number, and PHP will convert such strings automatically in arithmetic.

php · try it yourself
<?php
$inputs = ["42", "3.14", "-7", "1e3", " 12", "12abc", "abc", ""];

foreach ($inputs as $value) {
    printf("%-8s %s\n", '"' . $value . '"', is_numeric($value) ? "numeric" : "not numeric");
}

echo "10" + "5.5", "\n";
var_dump("10" + 5);
"42"     numeric
"3.14"   numeric
"-7"     numeric
"1e3"    numeric
" 12"    numeric
"12abc"  not numeric
"abc"    not numeric
""       not numeric
15.5
int(15)

Note: In PHP 8, arithmetic with a completely non-numeric string like "abc" + 1 throws a TypeError, and a leading-numeric string like "12abc" produces a warning. Validate input with is_numeric() or filter_var() before calculating.

Checking number types

Function True when
is_int($x) $x is an integer
is_float($x) $x is a float
is_numeric($x) $x is a number or a numeric string

Test yourself

What does var_dump(PHP_INT_MAX + 1); show the type as?

  1. int (it wraps to a negative number)
  2. float
  3. string
  4. It throws an OverflowError

Why is 0.1 + 0.2 == 0.3 false?

  1. PHP has a bug in addition
  2. Floats are binary approximations, so the sum is 0.30000000000000004
  3. == cannot compare floats
  4. 0.1 is stored as a string

Which value makes is_numeric() return false?

  1. "1e3"
  2. "-7"
  3. "12abc"
  4. "3.14"