A float (floating-point number) is a number with a decimal part.
<?php
$price = 19.99;
$pi = 3.14159;
$small = 0.5;
var_dump($price, $pi, $small);
float(19.99)
float(3.14159)
float(0.5)
Scientific notation
Very large or very small numbers can be written with e.
<?php
var_dump(1.5e3); // 1.5 * 10^3
var_dump(2e-3); // 2 * 10^-3
float(1500)
float(0.002)
Floats are approximate
Computers store floats in binary, and many decimal values cannot be stored exactly. This is true in every programming language, not only PHP.
<?php
var_dump(0.1 + 0.2);
var_dump(0.1 + 0.2 == 0.3);
float(0.30000000000000004)
bool(false)
So never compare floats with ==. Compare with a small tolerance instead.
<?php
$a = 0.1 + 0.2;
$b = 0.3;
var_dump(abs($a - $b) < 0.00001);
bool(true)
Floating-Point Pitfalls goes further, including how to handle money.
Rounding for display
round() and number_format() are made for showing floats to people.
<?php
$price = 19.987;
echo round($price, 2) . "\n";
echo number_format($price, 2);
19.99
19.99
Special float values
PHP also has INF (infinity) and NAN (not a number). Check for them with is_infinite() and is_nan().
Do not store money as floats
0.1 + 0.2 is not exactly 0.3, and small errors add up. Store prices as integers in the smallest unit (cents or paise), or use a decimal library.