Definition and Usage
- PHP Version
- 4+
The is_float() function checks if a given value is of the float type.
It returns true if the value is a float, and false otherwise.
The float type represents a data type used to express numeric values with decimal points.
In PHP, although integers and strings can be treated as numbers depending on the context, the float type itself always represents numbers with decimal points or the numeric results of operations that involve decimal points.
Basic Example
/* Floating-point numbers */
var_dump(is_float(3.14)); // bool(true)
var_dump(is_float(-10.5)); // bool(true)
var_dump(is_float(0.0)); // bool(true)
/* Integers */
var_dump(is_float(1)); // bool(false)
var_dump(is_float(0)); // bool(false)
var_dump(is_float(-10)); // bool(false)
/* Strings */
var_dump(is_float('3.14')); // bool(false)
var_dump(is_float('0.0')); // bool(false)
/* Results of numeric operations */
var_dump(is_float(1.0 + 2)); // bool(true)
var_dump(is_float(3 / 2)); // bool(true)
var_dump(is_float(5 + 2)); // bool(false)
/* The result of floating-point number operations is treated as a float type */
var_dump(1 + 1.0); // float(2)
var_dump(is_float(1 + 1.0)); // bool(true)
var_dump(0.5 + 0.5); // float(1)
var_dump(is_float(0.5 + 0.5)); // bool(true)
var_dump(2 * 3.5); // float(7)
var_dump(is_float(2 * 3.5)); // bool(true)
/* Type Casting */
var_dump(is_float((float) '3.14')); // bool(true)
var_dump(is_float((float) 1)); // bool(true)
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_float(mixed $value): bool
Parameters
$value |
Required. The value to be checked for the float type. |
|---|
Return Values
If the value passed as a parameter is of the float type, the function returns true; otherwise, it returns false.
Things to Keep in Mind
There are several important points to be aware of when using the is_float() function.
A String Representing a Floating-Point Number, Such as '10.5', Will Return false
$float = '10.5';
var_dump(is_float($float)); // bool(false)
Even If the Decimal Part Is Represented as 0, Such as 10.0, It Will Still Return true
$float = 10.0;
var_dump(is_float($float)); // bool(true)
The Result of Arithmetic Operations Involving Floating-Point Numbers Is Treated as a Float Type in PHP
$result = 0.5 + 0.5; // Expected result is 1
var_dump($result); // float(1) => Note that it's not an integer (int)!
var_dump(is_float($result)); // bool(true)
In PHP, the result of arithmetic operations involving floating-point numbers is processed as a float type by default. Additionally, when performing operations between floating-point numbers and integers, the result is automatically typecast to a floating-point number.
$float_result = 6.0 / 2;
var_dump($float_result); // float(3)
Practical Examples
The is_float() function is useful in situations where you need to check whether a value is of the integer type.
Checking if a Variable is of Float Type
It is used when you want to check whether a variable is a floating-point number. This is particularly useful when checking the type of data received from user input or external sources.
$value = 3.14;
if (is_float($value)) {
echo 'This value is a floating-point number.';
} else {
echo 'This value is not a floating-point number.';
}
// Output: 'This value is a floating-point number.'
Improving the Readability of Floating-Point Output
When displaying floating-point numbers, you can use the number_format() function to specify a certain number of decimal places, improving readability for users. For example, the following code checks if $number is a floating-point number, and if so, outputs it with two decimal places.
$number = 3.1415926535;
if (is_float($number)) {
echo number_format($number, 2); // Output: 3.14
} else {
echo $number;
}
By structuring the code this way, if the input data is not a floating-point number, the original value will be displayed. This improves readability while considering the data type of the input.
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_bool() Function – Checking the Boolean Type of a Variable
- PHP is_int() Function – Checking the Integer Type of a Variable
- PHP is_numeric() Function – Checking Whether a Variable’s Value Is a Number or a Numeric String
- PHP is_string() Function – Checking the String Type of a Variable
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide