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.
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!