Home arrow PHP arrow Page 4 - Using Filters in PHP 5

Using the FILTER_VALIDATE_INT filter to check for minimal and maximal values - PHP

You've probably built custom functions and libraries to validate user input in PHP 5. There's an easier way to do this, and it can save you considerable time and effort. It involves using PHP 5's own built-in and often-neglected filters. This nine-part series will introduce you to their use.

TABLE OF CONTENTS:
  1. Using Filters in PHP 5
  2. Filters available in PHP 5
  3. Checking for integer values with the filter PHP extension
  4. Using the FILTER_VALIDATE_INT filter to check for minimal and maximal values
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
July 15, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Since you learned in the previous segment how to use the FILTER_VALIDATE_INT filter to validate integers, Iím now going to explain another way to use it. This time we'll check not only if a given value is an integer, but if itís within a specified range. How can this be done? Itís simple, really; adding an optional array to the ďfilter_var()Ē function allows you to specify minimal and maximal boundaries for a determined value.

The following code samples show in a nutshell how to accomplish this task in a truly intuitive way. Take a look at them:

 

$min = 1;

$max = 99;

$input = 101;

if(filter_var($input, FILTER_VALIDATE_INT, array("options" => array("min_range" => $min, "max_range"=> $max ))) === FALSE) // displays Error: Input must be a value between 1 and 99 (Correct specification for the min and max options)

{

echo 'Error: Input must be a value between 1 and 99.';

}

else

{

echo 'Input is correct';

}

 

$min = 1;

$max = 99;

$input = 101;

if(filter_var($input, FILTER_VALIDATE_INT, array("options" => array("max_range"=> $max ))) === FALSE) // displays Error: Input must be a value between 1 and 99 (Specifies only the max option)

{

echo 'Error: Input must be a value between 1 and 99.';

}

else

{

echo 'Input is correct';

}

As I said before, by adding an ďoptionsĒ array to the ďfilter_var()Ē function itís possible to check not only if the inputted value is an integer, but if it belongs to a specified range. In the first case, both minimal and maximal boundaries have been specified, while in the second example, only the upper limit has been set.

From the previous code samples, itís easy to see how useful the FILTER_VALIDATE_INT filter can be for validating integers. However, Iím only scratching the surface of the PHP 5 filter library; as you saw at the beginning, it comes packaged with many other handy filters. These will be discussed in successive articles of this series.

Final thoughts

In this initial chapter of the series, I introduced you to using the filter extension that comes included with PHP 5. As you were able to see in all of the code samples included previously, checking to see whether or not a specified value is an integer is an extremely straightforward process thanks to the help of the FILTER_VALIDATE_INT filter.

In the forthcoming article, Iím going to explain how to use the filter extension to validate array elements and to check both octal and hexadecimal integers. Donít miss the next part!



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