PHP Numbers
In this page:
$integer = 10;
$float = 10.5;
var_dump(PHP_INT_MAX); // largest integer
var_dump(is_int($value));
var_dump(is_float($value));
Integer and Float Types
PHP has two core numeric types: int for whole numbers and float (also called double) for numbers with a decimal point or in exponential notation.
PHP is dynamically typed, so a variable can hold an int in one line and a float the next -- the type is inferred from the literal you assign, not declared up front.
Example: Integer and Float Types
<?php
// Declare `$a`, set to `10`
$a = 10;
// Declare `$b`, set to `10.5`
$b = 10.5;
// Print a detailed dump (with types) of `$a`
var_dump($a);
// Print a detailed dump (with types) of `$b`
var_dump($b);
?>
Login to try C/C++/Java/PHP code in the editor
Integer Limits and Overflow
Every platform has a maximum integer size, exposed in PHP as the PHP_INT_MAX constant (typically 9223372036854775807 on 64-bit systems).
Adding 1 past that limit doesn't wrap around or error -- PHP silently promotes the result to a float, which can lose precision for very large numbers, so relying on exact integer arithmetic near that boundary is risky.
Example: Integer Limits and Overflow
<?php
// Print `PHP_INT_MAX . "\n"` to the output
echo PHP_INT_MAX . "\n";
// Print a detailed dump (with types) of `PHP_INT_MAX + 1`
var_dump(PHP_INT_MAX + 1);
?>
Login to try C/C++/Java/PHP code in the editor
Floating-Point Precision
Floats are stored in binary, and many decimal fractions -- like 0.1 -- can't be represented exactly in binary, the same issue every language built on IEEE 754 floats has.
This is why 0.1 + 0.2 == 0.3 can evaluate to false in PHP; comparing floats for exact equality is a common source of subtle bugs, and rounding to a fixed precision before comparing is the usual fix.
Example: Floating-Point Precision
<?php
// Print a detailed dump (with types) of `0.1 + 0.2 == 0.3`
var_dump(0.1 + 0.2 == 0.3);
// Print `round(0.1 + 0.2, 10) == round(0.3, 10) ? "Equal after rounding" : "Still unequal"` to the output
echo round(0.1 + 0.2, 10) == round(0.3, 10) ? "Equal after rounding" : "Still unequal";
?>
Login to try C/C++/Java/PHP code in the editor
Numeric Strings
PHP treats strings like "42" or "3.14" as numeric strings and will automatically convert them for arithmetic -- "5" + "3" evaluates to 8, not a string concatenation.
PHP 8 tightened the rules for what counts as a valid numeric string, and non-numeric strings used in arithmetic now throw a TypeError instead of silently becoming 0 as in older versions.
Example: Numeric Strings
<?php
// Print "5" + "3" to the output
echo "5" + "3";
?>
Login to try C/C++/Java/PHP code in the editor
Number Formatting and Conversion
Functions like number_format(), round(), intval(), and floatval() convert between formats and precision levels for display or storage.
number_format() is especially useful for adding thousands separators and a fixed decimal count when showing prices or large counts to users, something raw echo of a float won't do correctly.
Example: Number Formatting and Conversion
<?php
// Print `number_format(1234567.891, 2) . "\n"` to the output
echo number_format(1234567.891, 2) . "\n";
// Print `round(4.567, 1) . "\n"` to the output
echo round(4.567, 1) . "\n";
// Print `intval("42abc")` to the output
echo intval("42abc");
?>
Login to try C/C++/Java/PHP code in the editor
- Comparing floats with
==, like0.1 + 0.2 == 0.3, which isfalsebecause of binary precision; compare with a small tolerance instead. - Ignoring integer overflow: adding to
PHP_INT_MAXsilently turns the result into a float, so integer-only code behaves differently. - Assuming
intdiv()and/are the same, when10 / 4gives the float2.5and onlyintdiv(10, 4)gives2.
- PHP is introduced with its history, features, setup, and a first program with basic syntax and structure.
- Comments, keywords, and identifiers are the basic rules of PHP code.
- Data types, variables, constants, type casting, and numbers control how values are stored.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: