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.
blog comments powered by Disqus |