PHP is_float() Function

What is PHP is_float() Function?

If you want to check whether a value is of float type or not, use is_float() function. Instead of using value directly, you can use a variable to check its type too.

Syntax:

is_float(value)

Parameters:

The Function has 1 parameter which is required-

value (Required): It is the value or variable whose type to be evaluated. This parameter can accept any type of value/variable. Check example 1.

Return Values:

The function returns-

  • TRUE – if the value is of float type.
  • FALSE – if the value is not of type float.

Check example 2.

How to use is_float() in coding?

As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result the conditional statement returns, your program flow progresses. Have a look at the code-

<?php
if(is_float($Height)){
    echo "Height is: " . $Height;
}else{
    echo "Height must be float.";
}
?>

Examples:

Example 1: is_float() can check type of a value or a variable-

<?php
var_dump(is_float(35.35));
echo "<br />";
$lesson = 35.35;
var_dump(is_float($lesson));
?>

Output:

bool(true)
bool(true)

Example 2: is_float() function examples-

<?php
var_dump(is_float(1e3));
echo "<br />";
var_dump(is_float(NULL));
echo "<br />";
var_dump(is_float(FALSE));
echo "<br />";
var_dump(is_float(99));
echo "<br />";
var_dump(is_float(" "));
echo "<br />";
var_dump(is_float(""));
echo "<br />";
$arr = [2.4];
var_dump(is_float($arr));
echo "<br />";
class Language {var $abc = 20.50;}
$php = new Language();
var_dump(is_float($php));
?>

Output:

bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)

Why type checking is important?

Type checking is crucial part of any project. It can break your code if not done correctly.

  • Users may provide different type of data than they are asked to provide. For example, in a field, you users instructed to add float, but a user may add number of other data of different type. Though, you may validate this with client-side validation to prevent them entering non-float information, you must implement server-side validation too with PHP. And, you can do this with is_float() function.
  • As PHP can automatically change data type (Type Juggling) of a variable, you need to be careful situations like this when type conversion may occur. In the following code snippet, after concatenation with a number (1), the float variable grade becomes string (it outputs: string(5) “3.971”).
    <?php
    $grade = 3.97;
    $grade = $grade . 1;
    var_dump($grade);
    ?>
    

Practical Usages of is_float() Function:

  • Suppose you declared a table column named “Grade” which only accepts float. Before inserting data in this column, you can check the data type with the is_float() function. Check the code snippet below-
    <?php
    $Grade = $_POST['Grade'];
    if(is_string($Grade)){
        mysqli_query($connection, "INSERT INTO `student` (`Grade`, ...) 
                            VALUES ('$Grade',...)");
    }else{
        echo "Grade must be a float.";
    }
    ?>
    
  • Suppose users are instructed to add only float in the “Price” field. But, one user added integer or string instead of float. Before further processing this data like displaying it in the website, storing in the database etc, you should at first check its type with is_float() function.

Notes on is_string() Function:

The number of significant digits displayed in floating point numbers is 14.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_string() Function

is_float() is another useful built-in variable-handling function that helps you check whether a value is float or not. Use the function in your next PHP project when needed.

Reference:

https://www.php.net/manual/en/function.is-float.php