> ## 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.

# PHP float

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.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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).

```php theme={"dark"}
$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`.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
