Validating Boolean Values and Float Numbers with Filters in PHP 5

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.

If you ever thought that PHP 5 comes mostly from its object model, then fortunately I have to say that you’re wrong. The latest version of PHP comes packaged with many other handy features that have contributed enormously to making it the mature language that it is nowadays.

However, along with its popular improvements, PHP provides a powerful set of filters for performing all sorts of clever validation on variables. These filters can check integers and float numbers, email addresses and URLs, Boolean values and IPs and much, much more.

This group of filters, also commonly known as "filter extensions," allow you to check for incoming data by using an extremely friendly approach. So if you’re searching for a solid library that lets you perform strict validation on user-supplied parameters before doing something useful with them (like inserting them into a database table, for instance), then hopefully this series of articles might be what you need. In it, you’ll find a concise guide that will show you how to work with the most useful filters that come bundled with this extension.

And now that you know what to expect from these tutorials, it’s time to quickly review the concepts that were covered in the last one. In that specific installment of the series, I discussed the usage of the FILTER_VALIDATE_BOOLEAN filter. As its name suggests, it was used for checking Boolean values in conjunction with the already familiar "filter_var()" function.

In addition, it’s fair to note that the aforementioned filter was utilized for determining whether or not the value assigned to a single variable was Boolean. As you learned in previous parts of the series, the filter extension permits you to validate elements of a given array very easily as well. Therefore, in this fourth chapter I’m going to explain how to check for Boolean values in arrays, as well as how to validate float numbers using another handy filter.

Are you ready to continue exploring the numerous capabilities provided by the  PHP 5 filter extension? Then, let’s jump in!

{mospagebreak title=Review: the FILTER_VALIDATE_BOOLEAN filter}

Before I explain how to use the filter extension to validate Boolean values in arrays, as well as how to check float numbers, I’m going to review quickly the set of examples developed during the preceding article. Those were aimed at illustrating how to utilize the FILTER_VALIDATE_BOOLEAN filter to check whether or not the value assigned to a given variable is Boolean.

Having said that, here’s how these examples were created originally. Take a look at them, please:

 

// example on validating Boolean TRUE values with the FILTER_VALIDATE_BOOLEAN filter

 

echo filter_var(1, FILTER_VALIDATE_BOOLEAN); // displays 1

echo filter_var(’1′, FILTER_VALIDATE_BOOLEAN); // displays 1

echo filter_var(TRUE, FILTER_VALIDATE_BOOLEAN); // displays 1

echo filter_var(‘TRUE’, FILTER_VALIDATE_BOOLEAN); // displays 1

echo filter_var(true, FILTER_VALIDATE_BOOLEAN); // displays 1

echo filter_var(‘true’, FILTER_VALIDATE_BOOLEAN); // displays 1

echo filter_var(‘on’, FILTER_VALIDATE_BOOLEAN); // displays 1

echo filter_var(true, FILTER_VALIDATE_BOOLEAN); // displays 1

 

 

// example on validating Boolean false values with the FILTER_VALIDATE_BOOLEAN filter

 

echo filter_var(0, FILTER_VALIDATE_BOOLEAN); // displays nothing

echo filter_var(’0′, FILTER_VALIDATE_BOOLEAN); // displays nothing

echo filter_var(FALSE, FILTER_VALIDATE_BOOLEAN); // displays nothing

echo filter_var(‘FALSE’, FILTER_VALIDATE_BOOLEAN); // displays nothing

echo filter_var(‘off’, FILTER_VALIDATE_BOOLEAN); // displays nothing

echo filter_var(NULL, FILTER_VALIDATE_BOOLEAN); // displays nothing

echo filter_var(‘NULL’, FILTER_VALIDATE_BOOLEAN); // displays nothing

 

To be frank, determining whether a PHP variable has been assigned a Boolean value with the FILTER_VALIDATE_BOOLEAN filter is actually a no-brainer process that can be tackled with minor hassles. As you probably recall, the examples listed above show how this filter behaves when used with different values that will be evaluated either as TRUE or FALSE.

So far, so good. At this point I’m sure that you’re familiar with using the filter extension to validate Boolean data types. So, the next step that I’m going to take will consist of explaining how to check Boolean values in arrays, in this case by means of the "filter_var_array()" function that you learned in previous installments of the series.

This topic will be discussed in more detail in the next section. Therefore, to learn more about it, please click on the link that appears below and read the next few lines.

{mospagebreak title=Validating Boolean values in arrays}

As I stated in the introduction, the filter extension permits you to validate not only values assigned to a single variable, but to filter elements of an array in one round. In this particular case, I’m going to demonstrate how to perform this task by way of an additional constant called FILTER_REQUIRE_ARRAY, which will be used for validating Boolean values.

That being explained, here’s another code sample that shows how to use the aforementioned constant in a concrete case. Have a look at it:

// example on validating Boolean false values in arrays

 

$values = array(0, 1, 2, 3, 4, ‘FALSE’, FALSE, array(0, 1, 2, 3, 4, TRUE, ‘TRUE’));

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

var_dump($filteredValues);

 

/* displays the following

array

0 => boolean false

1 => boolean true

2 => boolean false

3 => boolean false

4 => boolean false

5 => boolean false

6 => boolean false

7 =>

array

0 => boolean false

1 => boolean true

2 => boolean false

3 => boolean false

4 => boolean false

5 => boolean true

6 => boolean true

*/

As illustrated by the above hands-on example, validating Boolean values in arrays is a straightforward process that can be grasped very quickly. In this specific situation, the values to check are of multiple types, including string and float numbers, other arrays and so forth, which are first evaluated with the "filter_var()" function in conjunction with the FILTER_REQUIRE_ARRAY constant, and then the corresponding results are displayed on screen via the "var_dump()" PHP function.

Well, at this point you should feel pretty satisfied, since you’ve learned how to check Boolean values in arrays by using the filter extension. Provided that you grasped the logic that drives this process, in the last section of this article I’m going to discuss how to utilize the extension for validating float numbers.

As usual, to see how this brand new validation process will be accomplished, click on the link that appears below and keep reading.

{mospagebreak title=Checking for float numbers}

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.’;

}

else

{

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);

var_dump($filteredValues);

 

/*

displays the following

array

0 => float 10.75

1 => float 10.75

2 => float -1234.5678

3 => boolean false

4 =>

array

empty

*/

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)));

var_dump($filteredValue);

}

 

/* 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!

Google+ Comments

Google+ Comments