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

Validating Boolean values in arrays - 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.

  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



As I stated in the introduction, the filter extension permits you to validate not only values assigned to a single variable, but to filter elements of an array in one round. In this particular case, I'm going to demonstrate how to perform this task by way of an additional constant called FILTER_REQUIRE_ARRAY, which will be used for validating Boolean values.

That being explained, here's another code sample that shows how to use the aforementioned constant in a concrete case. Have a look at it:

// example on validating Boolean false values in arrays


$values = array(0, 1, 2, 3, 4, 'FALSE', FALSE, array(0, 1, 2, 3, 4, TRUE, 'TRUE'));

$filteredValues = filter_var($values, FILTER_VALIDATE_BOOLEAN, FILTER_REQUIRE_ARRAY);



/* displays the following


0 => boolean false

1 => boolean true

2 => boolean false

3 => boolean false

4 => boolean false

5 => boolean false

6 => boolean false

7 =>


0 => boolean false

1 => boolean true

2 => boolean false

3 => boolean false

4 => boolean false

5 => boolean true

6 => boolean true


As illustrated by the above hands-on example, validating Boolean values in arrays is a straightforward process that can be grasped very quickly. In this specific situation, the values to check are of multiple types, including string and float numbers, other arrays and so forth, which are first evaluated with the "filter_var()" function in conjunction with the FILTER_REQUIRE_ARRAY constant, and then the corresponding results are displayed on screen via the "var_dump()" PHP function.

Well, at this point you should feel pretty satisfied, since you've learned how to check Boolean values in arrays by using the filter extension. Provided that you grasped the logic that drives this process, in the last section of this article I'm going to discuss how to utilize the extension for validating float numbers.

As usual, to see how this brand new validation process will be accomplished, click on the link that appears below and keep reading.

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