Home arrow PHP arrow Page 2 - Validating Boolean Values and Float Numbers with Filters in PHP 5

Review: the FILTER_VALIDATE_BOOLEAN filter - PHP

Welcome to the fourth part of a series covering the use of filters in PHP. In this article, you'll learn some very useful ways to take advantage of the capabilities of the filter extension to validate Boolean values in arrays; we'll also show you how to validate float numbers.

TABLE OF CONTENTS:
  1. Validating Boolean Values and Float Numbers with Filters in PHP 5
  2. Review: the FILTER_VALIDATE_BOOLEAN filter
  3. Validating Boolean values in arrays
  4. Checking for float numbers
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
August 05, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I explain how to use the filter extension to validate Boolean values in arrays, as well as how to check float numbers, I'm going to review quickly the set of examples developed during the preceding article. Those were aimed at illustrating how to utilize the FILTER_VALIDATE_BOOLEAN filter to check whether or not the value assigned to a given variable is Boolean.

Having said that, here's how these examples were created originally. Take a look at them, please:

 

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

 

 

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

 

To be frank, determining whether a PHP variable has been assigned a Boolean value with the FILTER_VALIDATE_BOOLEAN filter is actually a no-brainer process that can be tackled with minor hassles. As you probably recall, the examples listed above show how this filter behaves when used with different values that will be evaluated either as TRUE or FALSE.

So far, so good. At this point I'm sure that you're familiar with using the filter extension to validate Boolean data types. So, the next step that I'm going to take will consist of explaining how to check Boolean values in arrays, in this case by means of the "filter_var_array()" function that you learned in previous installments of the series.

This topic will be discussed in more detail in the next section. Therefore, to learn more about it, please click on the link that appears below and read the next few lines.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: