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

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: