What is a floating-point (float) data type?
Floating-point data types are our common decimals, like "0.7", "100.2" are floating-point data, floating-point data types can be used to store integers or to store decimals. It's more accurate than the integer data type we talked about earlier.
The valid range for floating-point is 1.8e-308~1.8e+308.
Before PHP4.0, floating-point identifiers are double, also called double-precision floating-point numbers, and they are no different.
Floating-point data has two default writing formats, a standard format , as follows
3.1415-35.8
There is also a scientific notation format
3.14*10^3 can use 3.14E3 to indicate
Floating-point (float) data type example
In the example below, we will test for different numbers. PHP Var_dump () returns the data type and value of the variable:
<?php$x = 10.3605;var_dump ($x), echo "<br>", $x = 2.4e3;var_dump ($x); echo "<br>"; $x = 8e-5;var_dump ($x);? >
Code Run Result:
Floating-point data is just an approximate value, so try to avoid the comparison size between floating-point numbers, because the final result is inaccurate. Like this.
<?php$a=0.1; $b =0.7;if (($a + $b) ==0.8) { echo "true";} Else{ echo ' false ';}? >
The result of the code run:
We talked about four scalar data types in PHP, Boolean (Boolean), String, Integer (integer), plus floating point (float) in this section. In the next section, we'll talk about "array" in PHP, which conforms to the data type.