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.

A float (floating-point number, double, or real number) in PHP is a scalar data type used to represent numeric values with a fractional component. PHP implements floats using the platform’s underlying C double type, which is almost universally the IEEE 754 double-precision 64-bit format. This provides a maximum value of approximately 1.8e308 and a precision of roughly 14 decimal digits. In PHP, the keywords float and double are historically synonymous and interchangeable, though float is the standard type declaration.

Syntax and Instantiation

Floats can be declared using standard decimal notation or exponential (scientific) notation. As of PHP 7.4, underscores can be used as numeric separators to improve readability.
$decimal = 1.234;
$negative = -5.67;

// Exponential notation
$exponential = 1.2e3;          // 1200.0
$exponential_negative = 7E-10; // 0.0000000007

// Numeric separators (PHP 7.4+)
$separated = 1_234.567_89;

Precision and Limitations

Because rational numbers like 0.1 or 0.7 do not have exact binary representations in base-2 fractions, PHP floats are subject to inherent precision loss during internal conversion and arithmetic operations.
$a = 0.1 + 0.2;
$b = 0.3;

var_dump($a === $b); 
// bool(false) 
// Internal representation of $a is approximately 0.30000000000000004

Comparison Strategy

Due to the aforementioned precision loss, direct equality comparisons (== or ===) between floats are unreliable. The standard architectural approach is to compare the absolute difference between two floats against an acceptable error margin. For the upper bound on relative error due to rounding in floating-point arithmetic, PHP provides the PHP_FLOAT_EPSILON constant (introduced in PHP 7.2). This constant represents the actual machine epsilon (the difference between 1.0 and the next representable floating-point number). Depending on the required precision, developers can use this machine epsilon or define an arbitrary tolerance (delta).
$a = 0.1 + 0.2;
$b = 0.3;

// Using PHP's built-in machine epsilon (PHP 7.2+)
$is_equal_machine = abs($a - $b) < PHP_FLOAT_EPSILON;
// bool(true)

// Using an arbitrary tolerance (delta)
$tolerance = 0.00001;
$is_equal_custom = abs($a - $b) < $tolerance; 
// bool(true)

Type Casting and Conversion

Variables of other types can be explicitly coerced into floats using the (float) or (double) cast operators, or via the floatval() function. When casting strings, PHP extracts the leading numeric portion; if no valid numeric sequence leads the string, it evaluates to 0.0.
$from_int = (float) 5;               // 5.0
$from_string = (float) "12.345ext";  // 12.345
$from_invalid = (float) "text12.3";  // 0.0
$from_bool = floatval(true);         // 1.0

Special Values (NAN and INF)

Operations that exceed the bounds of the float type or represent undefined mathematical operations result in special float constants. These cannot be compared using standard operators and require specific helper functions.
$infinity = PHP_FLOAT_MAX * 2; // INF
$negative_infinity = log(0);   // -INF
$not_a_number = acos(8);       // NAN

// Validation functions
var_dump(is_infinite($infinity));     // bool(true)
var_dump(is_nan($not_a_number));      // bool(true)
var_dump(is_finite(1.234));           // bool(true)

// NAN comparison anomaly
var_dump(NAN === NAN);                // bool(false) - NAN is never equal to NAN
Master PHP with Deep Grasping Methodology!Learn More