Home arrow PHP arrow Page 4 - Validating Boolean Values and Float Numbers with Filters in PHP 5

Checking for float numbers - PHP

Welcome to the fourth part of a series covering the use of filters in PHP. In this article, you'll learn some very useful ways to take advantage of the capabilities of the filter extension to validate Boolean values in arrays; we'll also show you how to validate float numbers.

  1. Validating Boolean Values and Float Numbers with Filters in PHP 5
  2. Review: the FILTER_VALIDATE_BOOLEAN filter
  3. Validating Boolean values in arrays
  4. Checking for float numbers
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
August 05, 2009

print this article



Definitely, a good epilogue for this tutorial consists of showing you how to use the filter extension to check whether or not the value assigned to a PHP variable is a float number. Of course, if you were a careful reader and paid close attention to the examples developed in preceding articles of the series, then you may imagine that this task will be performed through another constant, called FILTER_VALIDATE_FLOAT.

This constant, when used with the "filter_var()" function, permits you to validate floating numbers very simply. The following code samples demonstrate how to check float numbers using a single scalar variable and an array as well. Here they are:

(basic example on validating float numbers)


$floatValue = 1.23456;

if(filter_var($floatValue, FILTER_VALIDATE_FLOAT) === FALSE) // displays Input is a valid float number.


echo 'Input is not a valid float number.';




echo 'Input is a valid float number.';




(example on validating float numbers using an array of values)


$values= array(10.75, '10.75', -1234.5678, 'This is a string', array());

$filteredValues = filter_var($values, FILTER_VALIDATE_FLOAT, FILTER_REQUIRE_ARRAY);




displays the following


0 => float 10.75

1 => float 10.75

2 => float -1234.5678

3 => boolean false

4 =>




That was really simple to understand, wasn't it? As shown above, the filter extension doesn't care if the value being validated is a string or a real float number as long as it contains a decimal part. This was clearly demonstrated by the first example. With reference to the second one, it simply shows how to perform the same checking process using a trivial array. 

However, the functionality of the FILTER_VALIDATE_FLOAT filter doesn't stop here. It has the ability to specify what type of decimal notation will be used for validating float numbers. Does this sound a bit confusing? Fear not. Take a look at the following example, which should help to dissipate any possible doubts you might have on this:

(example on validating float numbers using a separator)


$values= array(10.75 => '.', '10,75' => ',');

foreach ($values as $value => $separator)


$filteredValue = filter_var($value, FILTER_VALIDATE_FLOAT, array('options' => array('decimal' => $separator)));




/* displays the following

float 10


float 10.75


Here you have it. Now, thanks to the help of an additional "options" parameter, it's possible to specify which character will be used for coding the decimal part of a float number. This can be really useful if you're developing a web application that will deal with different decimal notations.

And that's all for now regarding the use of the PHP filter extension for checking float numbers. As always, feel free to edit all of the code samples included in this article; in this way you can sharpen your skills even further when working with this powerful library.

Final thoughts

Over this fourth chapter of the series, I discussed the use of the FILTER_VALIDATE_FLOAT filter for validating float numbers in a truly straightforward fashion. Undeniably, what makes the filter extension so easy to work with is that in most cases, validating a specific type of data only requires coupling the "filter_var()" function with the corresponding filtering constant. It's that simple, really.

Moving forward, in the upcoming part I'm going to explore in depth another filter provided by the extension. It will be used to check whether the value assigned to a variable follows the pattern defined by a specified regular expression. Don't miss the next part!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: