Home arrow PHP arrow Page 2 - Checking Boolean Values with Filters in PHP 5

Review: the FILTER_VALIDATE_INT 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

In case you havenít read the previous article of the series, where I explained how to use the filter PHP 5 extension for checking integer values in arrays and their respective representations in the octal and hexadecimal systems, here are the pertinent code samples that demonstrate how to accomplish this task by using the FILTER_VALIDATE_INT filter. Take a look at them, please:

(example on using the filter_var_array() function with FILTER_VALIDATE_INT filter)

 

<?php

 

$values = array(1, '0.675', -123456, 'This is a string', "abcdef", array(1, 2, 3));

// create an array of filtered values

$filtered_values = filter_var_array($values, FILTER_VALIDATE_INT);

foreach($filtered_values as $key=>$value)

{

echo '<p>' . $key .' ---- ' . $value.'</p>';

}

 

 

/* displays the following

0 ---- 1

 

1 ----

 

2 ---- -123456

 

3 ----

 

4 ----

 

5 ---- Array

 

*/

?>

 

 

(example on validating hexadecimal integers)

 

<?php

 

// check hexadecimal integers

$hexValue = '0xf0';

echo filter_var($hexValue, FILTER_VALIDATE_INT, array('flags' => FILTER_FLAG_ALLOW_HEX));

// displays 240

?>

 

 

(example on validating hexadecimal integers)

 

<?php

 

// checking octal integers

$octValue = '7601';

echo filter_var($octValue, FILTER_VALIDATE_INT, array('flags' => FILTER_FLAG_ALLOW_OCTAL));

// displays 7601

?>

Undeniably, the above examples are very easy to follow, meaning that you shouldnít have major problems understanding how they work. The first case illustrates a simple usage of the handy ďfilter_var_array()Ē function along with the FILTER_VALIDATE_INT filter to check whether or not the elements of a given array are integers, while the other two examples show how to use the same filter to check integers represented in hexadecimal and octal systems respectively.

Well, now that you've surely grasped the logic that stands behind using the FILTER_VALIDATE_INT filter, itís time to continue exploring the numerous capabilities offered by the filter extension. With that idea in mind, in the course of the following section Iím going to discuss the usage of yet another helpful filter, which will come in handy for validating Boolean values.

As I said before, this particular topic will be discussed in depth in the following segment. So, if you want to learn more about it, 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
   

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: