Current Location: Home> Latest Articles> How to Use the is_double Function in Conjunction with filter_var to Validate Floating Point Inputs?

How to Use the is_double Function in Conjunction with filter_var to Validate Floating Point Inputs?

gitbox 2025-06-08

1. Introduction

is_double is a built-in PHP function used to check if a variable is a floating-point number (also known as a double). The filter_var function is a powerful tool for filtering and validating data, ensuring it meets a specific predefined format or rule. When we need to validate floating-point numbers from user input, combining these two functions helps ensure both accuracy and security of the input.


2. Using is_double to Validate Floating-Point Numbers

Let’s start by looking at the basic usage of the is_double function. is_double returns a boolean value that indicates whether a given variable is a floating-point number. If it is, it returns true, otherwise it returns false.

<?php  
$var = 3.14;  
<p>if (is_double($var)) {<br>
echo "This is a floating-point number";<br>
} else {<br>
echo "This is not a floating-point number";<br>
}<br>
?>

In the code above, we define a floating-point number $var and use is_double to check if it is a floating-point number. If it is, the output will be “This is a floating-point number”.


3. Using filter_var to Validate Floating-Point Numbers

The filter_var function provides a more flexible way to validate variables. By using the FILTER_VALIDATE_FLOAT filter, we can validate if the input value is a valid floating-point number.

<?php  
$var = "3.14";  
<p>if (filter_var($var, FILTER_VALIDATE_FLOAT)) {<br>
echo "This is a valid floating-point number";<br>
} else {<br>
echo "Invalid floating-point number";<br>
}<br>
?>

In the above code, we use filter_var to validate whether $var is a valid floating-point number. If it is, the output will be “This is a valid floating-point number”.


4. Combining is_double and filter_var

To ensure the accuracy of floating-point input, we can combine is_double and filter_var. First, use filter_var to validate the validity of the floating-point number, then use is_double to ensure the type is a floating-point number. This combination provides more rigorous input validation.

<?php  
$var = "3.14";  
<p>if (filter_var($var, FILTER_VALIDATE_FLOAT) && is_double((float)$var)) {<br>
echo "The input is a valid floating-point number";<br>
} else {<br>
echo "Invalid input or incorrect type";<br>
}<br>
?>

In this example, we first use filter_var to validate if $var is a valid floating-point number. Then, we use is_double to check if its type is a floating-point number.


5. Combining URL Handling and Floating-Point Numbers

Sometimes, when validating floating-point numbers, we might be dealing with URLs. For example, a floating-point value could be passed via a URL parameter. We can combine filter_var's FILTER_VALIDATE_URL to validate the URL format, and then extract the floating-point number from it.

<?php  
$url = "http://gitbox.net/test.php?value=3.14";  
<p>// Use filter_var to validate if the URL is valid<br>
if (filter_var($url, FILTER_VALIDATE_URL)) {<br>
// Extract URL parameters<br>
$url_components = parse_url($url);<br>
parse_str($url_components['query'], $params);<br>
$value = $params['value'];</p>
<pre class="overflow-visible!"><div class="contain-inline-size rounded-2xl border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-2xl">php</div><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-sidebar-surface-primary text-token-text-secondary dark:bg-token-main-surface-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><button class="flex gap-1 items-center select-none py-1" aria-label="复制"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="icon-xs"><path fill-rule="evenodd" clip-rule="evenodd" d="M7 5C7 3.34315 8.34315 2 10 2H19C20.6569 2 22 3.34315 22 5V14C22 15.6569 20.6569 17 19 17H17V19C17 20.6569 15.6569 22 14 22H5C3.34315 22 2 20.6569 2 19V10C2 8.34315 3.34315 7 5 7H7V5ZM9 7H14C15.6569 7 17 8.34315 17 10V15H19C19.5523 15 20 14.5523 20 14V5C20 4.44772 19.5523 4 19 4H10C9.44772 4 9 4.44772 9 5V7ZM5 9C4.44772 9 4 9.44772 4 10V19C4 19.5523 4.44772 20 5 20H14C14.5523 20 15 19.5523 15 19V10C15 9.44772 14.5523 9 14 9H5Z" fill="currentColor"></path></svg>复制</button><button class="flex items-center gap-1 py-1 select-none"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="icon-xs"><path d="M2.5 5.5C4.3 5.2 5.2 4 5.5 2.5C5.8 4 6.7 5.2 8.5 5.5C6.7 5.8 5.8 7 5.5 8.5C5.2 7 4.3 5.8 2.5 5.5Z" fill="currentColor" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M5.66282 16.5231L5.18413 19.3952C5.12203 19.7678 5.09098 19.9541 5.14876 20.0888C5.19933 20.2067 5.29328 20.3007 5.41118 20.3512C5.54589 20.409 5.73218 20.378 6.10476 20.3159L8.97693 19.8372C9.72813 19.712 10.1037 19.6494 10.4542 19.521C10.7652 19.407 11.0608 19.2549 11.3343 19.068C11.6425 18.8575 11.9118 18.5882 12.4503 18.0497L20 10.5C21.3807 9.11929 21.3807 6.88071 20 5.5C18.6193 4.11929 16.3807 4.11929 15 5.5L7.45026 13.0497C6.91175 13.5882 6.6425 13.8575 6.43197 14.1657C6.24513 14.4392 6.09299 14.7348 5.97903 15.0458C5.85062 15.3963 5.78802 15.7719 5.66282 16.5231Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path><path d="M14.5 7L18.5 11" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg>编辑</button></div></div></div><div class="overflow-y-auto p-4" dir="ltr">// Use filter_var to validate the floating-point number  
if (filter_var($value, FILTER_VALIDATE_FLOAT) && is_double((float)$value)) {  
    echo "The floating-point value in the URL is valid";  
} else {  
    echo "The floating-point value in the URL is invalid";  
}  

} else {
echo "Invalid URL";
}
?>

In this example, we first validate the URL's validity, then extract the floating-point value from the URL and validate it. The domain part of the URL is replaced with gitbox.net to fit the requirements of the example.


6. Conclusion

By combining is_double and filter_var, we can easily validate floating-point number inputs. filter_var provides robust validation functionality, while is_double helps ensure type accuracy. In cases involving URLs, we can first validate the URL, extract the floating-point value, and then perform further validation to ensure the safety and accuracy of the input data.

Using this approach allows us to better manage and control user input, reducing potential errors or security risks.