PHP Numbers
PHP Numbers: Integers, Floats, and Type Casting
PHP numbers fall into two scalar types: int for whole numbers and float for decimals. PHP automatically converts between them during calculations, which is convenient but can lead to rounding errors, silent casts, and integer overflow. This guide explains how both types behave, how to validate numeric input safely, and how to avoid the mistakes that most often cause wrong totals in real applications.
What Are PHP Numbers?
PHP has no separate types for "small number" or "big number." You work with two:
int:whole numbers such as42, -7, or0.float:numbers with a fractional part, such as19.99or-0.5, including scientific notation like1.5e3.
You can write integer literals in several ways, and since PHP 7.4, you can use underscores to make long numbers readable:
<?php
$decimal = 1_000_000; // underscores are ignored by PHP
$hex = 0x1A; // 26
$binary = 0b1010; // 10Integers vs Floating-Point Numbers
On 64-bit servers, a PHP int holds whole numbers roughly up to 9.22e18 (PHP_INT_MAX). If an integer operation exceeds PHP_INT_MAX, PHP promotes the result to a float automatically.
<?php
declare(strict_types=1);
$stockCount = 42;
$unitPrice = 19.99;
$totalValue = $stockCount * $unitPrice; // int * float produces float
var_dump(is_int($stockCount));
var_dump(is_float($totalValue));
echo "Max 64-bit int: " . PHP_INT_MAX . PHP_EOL;
Floating-point numbers follow the IEEE 754 standard, which gives wide range at the cost of tiny binary rounding differences. Never use raw floats to store exact bank balances or invoice totals; store integer cents instead.
How to Check That Your Number Handling Works
Run
php -vto confirm the PHP version, andphp -r'echo PHP_INT_SIZE;' to confirm 32-bit or 64-bit.Test validation with awkward inputs:
"","abc","42abc","1e5"," 12 ","-5", and"12.5". Write down what you expect, then compare with the actual result.Use
var_dump()instead ofechowhile debugging, so you see both the type and the full float precision.Test boundary cases:
0negative numbers,PHP_INT_MAX, and zero as a divisor.If your app handles money, write a quick test that sums a list of prices as integer cents and compares the result with a hand-calculated total.
Common Mistakes to Avoid
Relying on
(int) "42abc"without validating: Casting"42abc"silently truncates to42. Validate withctype_digit()orfilter_var()first.Expecting
0.1 + 0.2 === 0.3: Binary floats produce0.30000000000000004. Compare rounded values or work with integer cents (10 + 20 === 30).Forgetting that
is_numeric()accepts scientific notation:"1e5"returnstrueforis_numeric(). Usectype_digit()when you only allow plain digits0-9.
Frequently Asked Questions
What happens when you divide two integers in PHP?
If the result has no remainder (10 / 2), PHP returns an int (5). If there is a remainder (10 / 3), PHP returns a float (3.3333333333333). Use intdiv(10, 3) for integer division.
What does PHP_FLOAT_EPSILON represent?
It is the smallest representable positive difference between 1.0 and the next larger float, useful when comparing floating-point values for near-equality.
How do I format a number as currency for display?
Use number_format($amount, 2, '.', ',') for simple formatting, or PHP's NumberFormatter class (intl extension) for locale-aware currency output.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.