Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP Floating Point Data Type
In PHP, the float data type represents any number with a decimal point or fractional part. Float values can be written in standard decimal notation or scientific notation using e or E. The size and precision of floats depend on the platform, though precision up to 14 decimal digits is commonly supported.
Syntax
// Standard decimal notation $var = 5327.496; // Scientific notation (lowercase e) $var1 = 5.327496e3; // Scientific notation (uppercase E) $var2 = 5.327496E3; // Using underscore separator for readability (PHP 7.4+) $var3 = 5_327.496;
For better readability, float literals may use "_" as a separation symbol, which is ignored by the PHP parser during processing. This feature is available since PHP 7.4.
Standard Decimal Notation
The most common way to represent float values is using standard decimal notation ?
<?php $price = 99.95; $temperature = -12.5; $ratio = 0.75; echo "Price: " . $price . "<br>"; echo "Temperature: " . $temperature . "<br>"; echo "Ratio: " . $ratio . "<br>"; ?>
Price: 99.95 Temperature: -12.5 Ratio: 0.75
Scientific Notation
Scientific notation is useful for very large or very small numbers ?
<?php $large_num = 5.327496e3; // 5327.496 $small_num = 2.5e-4; // 0.00025 $uppercase = 1.23E6; // 1230000 echo "Large: " . $large_num . "<br>"; echo "Small: " . $small_num . "<br>"; echo "Uppercase: " . $uppercase . "<br>"; ?>
Large: 5327.496 Small: 0.00025 Uppercase: 1230000
Underscore Separator
Note: Underscore separator in numeric literals requires PHP 7.4 or higher.
PHP 7.4 introduced underscore separators for improved readability of large numbers ?
<?php $readable = 1_234_567.89; $scientific = 1.234_567e6; echo "Readable: " . $readable . "<br>"; echo "Scientific: " . $scientific . "<br>"; ?>
Readable: 1234567.89 Scientific: 1234567
Type Checking
You can verify if a variable contains a float value using the is_float() function ?
<?php $float_val = 3.14; $int_val = 42; var_dump(is_float($float_val)); // bool(true) var_dump(is_float($int_val)); // bool(false) ?>
bool(true) bool(false)
Conclusion
PHP floats support standard decimal notation, scientific notation, and underscore separators for readability. Use scientific notation for very large or small numbers, and remember that underscore separators are only available in PHP 7.4 and later versions.
