Using PHP’s floatval Function to Convert Variables to Float

PHP’s built‑in floatval function converts various variable types—including integers, strings, booleans, and arrays—into floating‑point numbers, returning the numeric value or 0 for non‑numeric strings, making it useful for calculations and monetary operations.

php Courses
php Courses
php Courses
Using PHP’s floatval Function to Convert Variables to Float

In PHP, converting variables to floating‑point numbers is common for numerical calculations and monetary transactions. The language provides a built‑in function called floatval ( mixed $var ) : float that quickly casts a variable to a float.

The function accepts a single argument $var, which can be any PHP variable such as an integer, string, boolean, or array. It attempts to convert the value to a float and returns the result.

Below are example code snippets demonstrating how to use floatval with different data types:

// Convert an integer to float<br/>$int = 10;<br/>$float = floatval($int);<br/>echo $float;      // outputs: 10.0<br/>echo gettype($float); // outputs: double<br/><br/>// Convert a numeric string to float<br/>$str = "3.14";<br/>$float = floatval($str);<br/>echo $float;      // outputs: 3.14<br/>echo gettype($float); // outputs: double<br/><br/>// Convert a string with extra characters (non‑numeric part is ignored)<br/>$str = "3.14test";<br/>$float = floatval($str);<br/>echo $float;      // outputs: 3.14<br/>echo gettype($float); // outputs: double<br/><br/>// Convert a boolean to float<br/>$bool = true;<br/>$float = floatval($bool);<br/>echo $float;      // outputs: 1.0<br/>echo gettype($float); // outputs: double<br/><br/>// Convert an array (the first element is used)<br/>$arr = [5, 10, 15];<br/>$float = floatval($arr);<br/>echo $float;      // outputs: 5.0<br/>echo gettype($float); // outputs: double

These examples show that regardless of whether the input is an integer, string, boolean, or array, floatval can conveniently convert it to a floating‑point number.

It is important to note that if the variable cannot be converted to a float—such as a string containing non‑numeric characters—the function returns 0. Therefore, you should ensure the variable’s type and value are appropriate before using the function.

In summary, PHP’s floatval function provides a quick and reliable way to cast variables to floats, which is especially useful in numerical calculations and financial processing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

type conversionphp-functionsfloatval
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.