← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 13 of 13

PHP Numbers

PHP numbers come in two flavors: whole numbers like 7, and numbers with a decimal point like 7.5. It is like counting whole apples versus measuring a cup of flour.
Syntax
php
$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
<?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);
?>

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
<?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);
?>

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
<?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";
?>

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
<?php
// Print "5" + "3" to the output
echo "5" + "3";
?>

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
<?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");
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Comparing floats with ==, like 0.1 + 0.2 == 0.3, which is false because of binary precision; compare with a small tolerance instead.
  2. Ignoring integer overflow: adding to PHP_INT_MAX silently turns the result into a float, so integer-only code behaves differently.
  3. Assuming intdiv() and / are the same, when 10 / 4 gives the float 2.5 and only intdiv(10, 4) gives 2.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.