Home arrow PHP arrow 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.

  1. Checking Boolean Values with Filters in PHP 5
  2. Review: the FILTER_VALIDATE_INT filter
  3. Validating Boolean values with the FILTER_VALIDATE_BOOLEAN filter
  4. Evaluating FALSE values with the filter_var() function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 0
July 29, 2009

print this article



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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: