HomePHP Page 3 - Checking Boolean Values with Filters in PHP 5
Validating Boolean values with the FILTER_VALIDATE_BOOLEAN filter - 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.
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
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.