PHP Variable Handling floatval() Function



The PHP Variable Handling floatval() function is used to to get the float value of a variable. A float is a number with a decimal point, as 3.14 or 10.5. This function is important for making sure a variable is seen as a float in your program.

The function converts numbers, strings, and other objects to floats. For example, if you enter "10.5" as a string, 10.5 is returned as a float. But you should not use objects with this function because they will generate an error and return 1.

If you pass an empty array to floatval(), it will return zero. For a non-empty array, it will return 1. If you pass a string, its start point determines the result. It normally returns zero, unless the string starts with a number.

This method is compatible with PHP 4.2 and following versions. It is simple to use and is commonly used while working with calculations or numerical data.

Syntax

Below is the syntax of the PHP Variable floatval() function −

float floatval ( mixed $value )

Parameters

This function accepts $value parameter which is the variable whose corresponding float value will be returned. It maybe a scalar type. Do not pass objects to floatval() function. Doing so will produce an E_NOTICE level error and return 1.

Return Values

This function returns float value of a given variable.

  • If empty array is passed, floatval() returns 0.
  • If non-empty array is passed, floatval() returns 1.
  • If non-empty array is passed, return 1.
  • If String is passed, the floatval() will most likely return 0 although this depends on the leftmost characters of the string.

PHP Versions

This function works with PHP 4.2 and all later versions, including PHP 5, 7, and 8.

Example 1

First we will show you the basic example of the PHP Variable Handling floatval() function to get the float value of the given number.

<?php
   // Define a variable here
   $var = '51.76127E3'; 

   // Use floatval() function here
   $float_value = floatval($var);

   // Print the value
   echo "Float value of the given number is: ".$float_value; 
?>

Output

Here is the outcome of the following code −

Float value of the given number is: 51761.27

Example 2

In the below PHP code we will try to use the floatval() function and get the float value of the given variable.

<?php
   // Define a variable here
   $var = '56.138mln'; 

   // Use floatval() function here
   $float_value = floatval($var);

   // Print the value
   echo "Float value of the given number is: ".$float_value; 
?> 

Output

This will generate the below output −

Float value of the given number is: 56.138

Example 3

Now the below code uses an empty and one non empty array to get the float values by using the floatval() function and prints the values.

<?php
   // Define an empty array
   $emptyArray = [];

   // Define a non empty array
   $nonEmptyArray = [1, 2, 3];

   // Use floatval() on the arrays
   $float_empty = floatval($emptyArray);
   $float_non_empty = floatval($nonEmptyArray);

   // Print the results
   echo "Float value of the empty array: " . $float_empty . "\n";
   echo "Float value of the non-empty array: " . $float_non_empty . "\n";
?> 

Output

This will create the below output −

Float value of the empty array: 0
Float value of the non-empty array: 1

Example 4

The floatval() function converts variables into float values for mathematical operations. The code is below:

<?php
   // Define variables
   $num1 = "10.5";
   $num2 = "2.3abc";
   $array = [5, 10, 15];

   // Use floatval() in calculations
   $result = floatval($num1) * floatval($num2) + floatval($array);

   // Print the result
   echo "The result of the calculation is: " . $result;
?> 

Output

Following is the output of the above code −

The result of the calculation is: 25.15
php_variable_handling_functions.htm
Advertisements