Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An int (integer) in PHP is a scalar data type representing a whole number belonging to the mathematical set of integers (..., -2, -1, 0, 1, 2, ...) without a fractional or decimal component. PHP integers are strictly signed; the language does not support unsigned integers.

Internal Representation and Limits

The size of an int is platform-dependent, determined by the architecture of the system running PHP.
  • On 32-bit systems, the maximum value is typically 2147483647.
  • On 64-bit systems, the maximum value is typically 9223372036854775807.
PHP provides predefined constants to introspect integer limits at runtime:
  • PHP_INT_SIZE: The size of an integer in bytes.
  • PHP_INT_MAX: The largest supported integer.
  • PHP_INT_MIN: The smallest supported integer.

Integer Overflow

PHP does not throw an error or wrap around upon integer overflow. If an operation results in a value outside the bounds of the int type, PHP automatically promotes the result to a float. Once promoted, the value is subject to standard floating-point formatting rules. Functions like var_dump() (which relies on the precision INI directive) or var_export() (which relies on the serialize_precision INI directive) will display the value as a standard float. The output will only use scientific notation if the number’s magnitude exceeds the configured precision threshold.
$max = PHP_INT_MAX;
var_dump($max);      // int(9223372036854775807) on a 64-bit system

$overflow = $max + 1;
var_dump($overflow); // float(9.223372036854776E+18) - Formatted according to float precision rules

Syntax and Literals

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation. As of PHP 7.4, numeric literal separators (_) can be used to improve readability.
$decimal = 255;
$hexadecimal = 0xFF;       // Prefix 0x
$binary = 0b11111111;      // Prefix 0b
$octal_legacy = 0377;      // Prefix 0 (Legacy)
$octal_explicit = 0o377;   // Prefix 0o (PHP 8.1+)

$separated = 1_000_000;    // Evaluates to 1000000

Type Casting and Coercion

Values of other types can be explicitly converted to an integer using the (int) or (integer) casts, or via the intval() function.
  • From Float: The fractional part is dropped (truncated towards zero). Note that casting floats that exceed the bounds of an integer results in undefined behavior prior to PHP 8.0, and well-defined behavior resulting in exactly 0 in PHP 8.0+.
  • From Boolean: false yields 0, and true yields 1.
  • From Null: Yields 0.
  • From String: If the string starts with numeric data, it resolves to that number. If it does not contain valid numeric data, it resolves to 0. Explicit casting via (int) or intval() performs this conversion silently. Warnings or TypeErrors are only emitted during implicit coercion (such as arithmetic operations) with non-well-formed numeric strings in PHP 8.0+.
$from_float = (int) 3.99;        // int(3)
$from_float_neg = (int) -3.99;   // int(-3)
$from_bool = (int) true;         // int(1)
$from_null = (int) null;         // int(0)

$from_string = (int) "42";       // int(42)
$from_mixed = (int) "42 apples"; // int(42) - Silently converts
$from_invalid = (int) "apple";   // int(0)  - Silently converts

$out_of_bounds = (int) 1e20;     // int(0)  - PHP 8.0+ behavior

Division Behavior

Unlike languages where integer division yields an integer, the / operator in PHP yields a float if the result is not an exact integer. To perform strict integer division (discarding the remainder), the intdiv() function must be used.
$standard_div = 10 / 3;       // float(3.3333333333333)
$integer_div = intdiv(10, 3); // int(3)
Master PHP with Deep Grasping Methodology!Learn More