HomePHP Page 4 - Checking Boolean Values with Filters in PHP 5
Evaluating FALSE values with the filter_var() function - PHP
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.
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
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!