PHP handles numbers primarily through two scalar types: int for whole numbers and float for decimal numbers. PHP automatically converts between integers and floats when division or large calculations require decimal precision.
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.
Data coming from HTTP requests or CSV files starts as strings. Check whether a string represents a valid number with is_numeric() or filter_var(), then cast it explicitly with (int) or (float).
<?php
declare(strict_types=1);
$rawQty = "15";
$rawDiscount = "12.5";
$qty = is_numeric($rawQty) ? (int) $rawQty : 0;
$discount = filter_var($rawDiscount, FILTER_VALIDATE_FLOAT) ?: 0.0;
echo "Quantity: {$qty}, Discount: {$discount}%" . PHP_EOL;
echo "NaN check: " . (is_nan(acos(2.0)) ? 'NaN' : 'Valid') . PHP_EOL;
(int) "42abc" without validating: Casting "42abc" silently truncates to 42. Validate with ctype_digit() or filter_var() first.0.1 + 0.2 === 0.3: Binary floats produce 0.30000000000000004. Compare rounded values or work with integer cents (10 + 20 === 30).is_numeric() accepts scientific notation: "1e5" returns true for is_numeric(). Use ctype_digit() when you only allow plain digits 0-9.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.
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.
Use number_format($amount, 2, '.', ',') for simple formatting, or PHP's NumberFormatter class (intl extension) for locale-aware currency output.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.