Checking Boolean Values with Filters in PHP 5

Among the numerous libraries that come included by default with the PHP 5 distribution, there’s one that has been overlooked by many developers. This is a shame, because it can be really useful for validating incoming data in a truly simple fashion. I’m talking about the filter extension, a powerful set of functions that let programmers apply several filters to multiple PHP variables to perform all sorts of clever validations, ranging from checking for integer and float numbers, to verifying email addresses and URLs.

Therefore, if you feel curious and wish to learn how to incorporate the filter extension into your web developer’s toolbox, then don’t hesitate anymore; start reading this group of articles now!

Welcome to the third part of the series on using filters in PHP 5. Comprised of seven tutorials, this series gently introduces you to using the filter library that comes packaged with PHP 5. It shows you with numerous examples how to get the most out of each filter to perform strong validation on the inputted data handled by your web applications.

And now that you’re aware of the goal of this series, it’s time to review the topics that were covered in the last part of the series. So, in simple terms, during that particular article, I explained how to use the FILTER_VALIDATE_INT filter, in conjunction with the “filter_var()” function, to validate octal and hexadecimal integers. In both cases, a “flag” option was passed to the function to perform the data checking process.

Nonetheless, as I said before, the filter extension can be used for more than validating integers, since it provides the required functionality to checking other data types. Thus, in the next few lines I’m going to discuss how to use the extension for validating Boolean values. Does this sound good to you? Then please read the following section.

{mospagebreak title=Review: the FILTER_VALIDATE_INT filter}

In case you haven’t read the previous article of the series, where I explained how to use the filter PHP 5 extension for checking integer values in arrays and their respective representations in the octal and hexadecimal systems, here are the pertinent code samples that demonstrate how to accomplish this task by using the FILTER_VALIDATE_INT filter. Take a look at them, please:

(example on using the filter_var_array() function with FILTER_VALIDATE_INT filter)

 

<?php

 

$values = array(1, ‘0.675’, -123456, ‘This is a string’, "abcdef", array(1, 2, 3));

// create an array of filtered values

$filtered_values = filter_var_array($values, FILTER_VALIDATE_INT);

foreach($filtered_values as $key=>$value)

{

echo ‘<p>’ . $key .’ —- ‘ . $value.'</p>';

}

 

 

/* displays the following

0 —- 1

 

1 —-

 

2 —- -123456

 

3 —-

 

4 —-

 

5 —- Array

 

*/

?>

 

 

(example on validating hexadecimal integers)

 

<?php

 

// check hexadecimal integers

$hexValue = ‘0xf0′;

echo filter_var($hexValue, FILTER_VALIDATE_INT, array(‘flags’ => FILTER_FLAG_ALLOW_HEX));

// displays 240

?>

 

 

(example on validating hexadecimal integers)

 

<?php

 

// checking octal integers

$octValue = ‘7601’;

echo filter_var($octValue, FILTER_VALIDATE_INT, array(‘flags’ => FILTER_FLAG_ALLOW_OCTAL));

// displays 7601

?>

Undeniably, the above examples are very easy to follow, meaning that you shouldn’t have major problems understanding how they work. The first case illustrates a simple usage of the handy “filter_var_array()” function along with the FILTER_VALIDATE_INT filter to check whether or not the elements of a given array are integers, while the other two examples show how to use the same filter to check integers represented in hexadecimal and octal systems respectively.

Well, now that you’ve surely grasped the logic that stands behind using the FILTER_VALIDATE_INT filter, it’s time to continue exploring the numerous capabilities offered by the filter extension. With that idea in mind, in the course of the following section I’m going to discuss the usage of yet another helpful filter, which will come in handy for validating Boolean values.

As I said before, this particular topic will be discussed in depth in the following segment. So, if you want to learn more about it, click on the link that appears below and keep reading.

{mospagebreak title=Validating Boolean values with the FILTER_VALIDATE_BOOLEAN filter}

As I stated in the introduction of this article, the filter extension provides PHP developers with the capacity for determining whether a given value is or is not Boolean. This checking process can be easily accomplished with another handy filter called FILTER_VALIDATE_BOOLEAN, which can be used similarly to the FILTER_VALIDATE_INT constant that you learned in previous tutorials.

In this specific case, when the FILTER_VALIDATE_BOOLEAN filter is utilized in conjunction with the “filter_var()” function, it’ll return 1 if the value filtered is considered TRUE (more on this in a moment) and an empty string if the value in question is FALSE. So first, let me show you a bunch of examples where the FILTER_VALIDATE_BOOLEAN filter will return simply 1. Here they are: 

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

As you can see, the above examples show several cases where the FILTER_VALIDATE_BOOLEAN filter will consider a given value to be TRUE. Naturally, in all of these cases, the “filter_far()” function will return a value of 1, in this way providing us with a simple and effective mechanism for checking for TRUE Boolean values.

Now that I have explained how to utilize the FILTER_VALIDATE_BOOLEAN filter to validate variables that will return TRUE, it’s time to see how the same filter can be used for checking FALSE values. Therefore, if you wish to learn how this will be achieved, click on the link shown below and read the next few lines.

{mospagebreak title=Evaluating FALSE values with the filter_var() function}

In the prior section, you saw how to use the FILTER_VALIDATE_BOOLEAN filter along with the “filter_var()” function for evaluating Boolean TRUE values. However, as you might have guessed, this same combination can be utilized for checking FALSE entries.

To demonstrate this concept a bit more clearly, below I listed a bunch of hands-on examples that show different cases where the FILTER_VALIDATE_BOOLEAN filter will consider a given value to be FALSE. Please take some time and examine these code samples closely:

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

As shown above, when the “filter_var()” function is called with the FILTER_VALIDATE_BOOLEAN filter, it’ll return an empty string in all the cases where the filtered value is considered FALSE. Of course, from the previous example it’s clear to see that this includes the number 0 and the "0" string, the Boolean FALSE value and the string "FALSE," and finally the "off," NULL and "NULL" values as well.

Again, it’s valid to note that if you’re planning to use this filter to check whether or not the value assigned to a given variable is Boolean, you should keep in mind the behavior of the FILTER_VALIDATE_BOOLEAN filter in the cases demonstrated previously, to avoid eventual programming errors when developing your own PHP applications.

And with this last practical example, I’m finishing this third part of the series. As usual, feel free to edit all of the code samples developed in this tutorial, to give you a better understanding of how to use this powerful extension for validating Boolean values. The experience will be pretty instructive, trust me.

Final thoughts

Over the course of this third episode of the series, I provided you with a few interesting examples aimed at demonstrating how to work with the FILTER_VALIDATE_BOOLEAN filter to validate Boolean values. As you saw for yourself, there are different cases where the filter will evaluate a given variable as TRUE or FALSE, so be very careful about this behavior, particularly if you wish to use it within your PHP applications.

In the upcoming part, things will get more interesting yet, since I’m going to discuss the utilization of this handy filter for checking for Boolean values in arrays. Thus, now that you’re aware of the topic that will discussed in the forthcoming article, here’s my final recommendation: don’t miss it!

[gp-comments width="770" linklove="off" ]

chat