Numbers
Understand how PHP represents integers and floats, how numeric strings behave in arithmetic, and how to format numbers for display.
Introduction
Numbers are at the heart of nearly every program — calculating totals, counting items, applying discounts, tracking scores. PHP has two main numeric types, integers and floats, and it is fairly relaxed about converting between numbers and numeric-looking strings.
This lesson looks at how PHP represents integers and floats, what happens when an integer grows too large, how PHP treats "numeric strings" in calculations, how to check whether a value is actually numeric, and how to format numbers nicely for display using number_format().
- The difference between PHP's int and float types.
- What happens when an integer exceeds its maximum value (integer overflow).
- How PHP handles numeric strings like "42" or "3.14" in arithmetic.
- How to check numeric types with is_int(), is_float(), and is_numeric().
- How to format numbers for display with number_format().
Integers vs Floats
An integer (int) is a whole number with no decimal point — positive, negative, or zero. A float (short for "floating-point number", also called double) is a number that can have a fractional/decimal part. PHP automatically decides which type a number literal is based on how it is written.
<?php
$quantity = 5;
$price = 19.99;
$negative = -12;
$zero = 0;
var_dump($quantity);
var_dump($price);
echo gettype($quantity) . "\n";
echo gettype($price) . "\n";
?>int(5)
float(19.99)
integer
doublePHP internally refers to the float type as "double" (short for double-precision floating point), a naming holdover from PHP's C roots. is_float() and (float) are the modern, preferred names to use in your own code.
Dividing two integers can produce a float if the result is not a whole number — division in PHP always returns whatever type accurately represents the answer.
<?php
$a = 10 / 2;
$b = 10 / 3;
var_dump($a);
var_dump($b);
?>int(5)
float(3.3333333333333)Integer Overflow
PHP integers have a maximum size, exposed as the constant PHP_INT_MAX (on most modern 64-bit systems, this is 9,223,372,036,854,775,807). If a calculation produces a number larger than this, PHP does not throw an error — instead it automatically converts the result to a float, which can represent much larger (though less precise) values.
<?php
echo PHP_INT_MAX . "\n";
$max = PHP_INT_MAX;
$overflowed = $max + 1;
var_dump($overflowed);
?>9223372036854775807
float(9.2233720368548E+18)Because overflow silently converts to a float rather than raising an error, very large integer calculations can quietly lose precision. This rarely matters for everyday code, but it is worth knowing about if you ever work with huge counters or IDs.
Numeric Strings in Arithmetic
A "numeric string" is a string whose content looks like a number, such as "42" or "3.14". PHP is happy to use numeric strings directly in arithmetic — it converts them to actual numbers on the fly.
<?php
$a = "10";
$b = "5";
$sum = $a + $b;
var_dump($sum);
$price = "19.99";
$quantity = "3";
echo ($price * $quantity) . "\n";
?>int(15)
59.97If a string is not fully numeric — for example, it contains letters mixed with digits — using it in arithmetic will either produce a warning/error (in modern PHP) or use only the leading numeric portion, depending on the PHP version. Non-numeric strings used in math should always be avoided.
<?php
$age = "25 years";
// In PHP 8+, this raises a warning because "25 years" is not a valid numeric string
$nextYearAge = (int) $age + 1;
echo $nextYearAge . "\n";
?>26When a value might come from user input or an external source, explicitly cast it with (int) or (float) before doing math with it. This makes your intent clear and avoids relying on PHP's automatic string-to-number conversion rules.
Checking Numeric Types
PHP provides simple functions to check whether a value is a particular numeric type. is_int() (alias is_integer()) checks for a true integer, is_float() (alias is_double()) checks for a float, and is_numeric() checks whether a value — including a numeric string — could be treated as a number.
<?php
$a = 42;
$b = 3.14;
$c = "100";
$d = "hello";
var_dump(is_int($a));
var_dump(is_float($b));
var_dump(is_numeric($c));
var_dump(is_numeric($d));
var_dump(is_int($c));
?>bool(true)
bool(true)
bool(true)
bool(false)
bool(false)Notice that is_numeric("100") returns true (the string looks like a valid number), but is_int("100") returns false, because the value's actual type is a string, not an integer — is_int() checks the type, not just the appearance.
is_numeric() is especially useful right after receiving data from a form or URL, since values from $_GET and $_POST always arrive as strings. Checking is_numeric() before converting with (int) or (float) helps catch bad input early.
Formatting Numbers for Display
Raw numbers are great for calculations but often need reformatting before being shown to a user — adding thousands separators, rounding to a fixed number of decimal places, or displaying currency. number_format() handles all of this in one call.
<?php
$total = 1234567.891;
echo number_format($total) . "\n";
echo number_format($total, 2) . "\n";
echo number_format($total, 2, ",", ".") . "\n";
$price = 49.9;
echo "$" . number_format($price, 2) . "\n";
?>1,234,568
1,234,567.89
1.234.567,89
$49.90number_format() takes up to four arguments: the number itself, how many decimal places to show, the character used as the decimal point, and the character used as the thousands separator — which makes it easy to adapt to different regional number formats.
Common Mistakes
- Assuming form input ($_GET/$_POST values) is already numeric — it always arrives as a string.
- Comparing floats for exact equality (0.1 + 0.2 == 0.3 is false due to floating-point precision).
- Forgetting that number_format() returns a string, not a number, so it should only be used for display, not further math.
- Confusing is_numeric() (checks if a value could be a number) with is_int()/is_float() (checks the value's actual type).
Best Practices
- Cast user input explicitly with (int) or (float) once you have confirmed it with is_numeric().
- Use number_format() only for the final display value, never for numbers you plan to calculate with further.
- Avoid relying on exact float equality; if needed, compare with a small tolerance instead.
- Keep PHP_INT_MAX in mind when working with very large counters, IDs, or financial totals.
Frequently Asked Questions
What is the difference between is_numeric() and is_int()?
is_numeric() checks whether a value (including a string like "42") could be interpreted as a number. is_int() checks whether the value's actual PHP type is an integer, regardless of appearance.
Does PHP have a maximum float size?
Floats have an enormous range, but they trade precision for that range. For very large or very precise values (like money), consider storing amounts as integers (e.g. cents) or using an arbitrary-precision library.
Why did my integer suddenly become a float?
This usually happens due to overflow (exceeding PHP_INT_MAX) or because a division produced a non-whole result. Both are normal, automatic PHP behaviors.
Is "10" the same as 10 in PHP?
They are different types (string vs. integer), but PHP will treat "10" as the number 10 in arithmetic contexts because it is a valid numeric string.
How do I round a number instead of just formatting it?
number_format() rounds for display but returns a string. To get a rounded number you can still calculate with, use round() instead — covered in the next lesson.
Key Takeaways
- PHP has two core numeric types: int (whole numbers) and float (decimal numbers).
- Exceeding PHP_INT_MAX silently converts a value to a float instead of raising an error.
- Numeric strings like "42" are automatically converted to numbers in arithmetic.
- is_int(), is_float(), and is_numeric() let you check numeric types and validate input.
- number_format() formats a number for display with thousands separators and fixed decimal places, but returns a string.
Summary
Numbers in PHP are flexible: integers and floats convert into each other automatically, and even numeric strings can participate directly in calculations.
In this lesson, you learned the difference between integers and floats, what happens on overflow, how numeric strings behave in math, how to check numeric types, and how to format numbers for display. Next, you will explore PHP's built-in math functions in more depth.
- You understand the difference between PHP's int and float types.
- You know what happens when an integer overflows.
- You can safely work with numeric strings in arithmetic.
- You can validate and format numbers for real-world use.