Home arrow PHP arrow Page 2 - Validating Octal and Hexadecimal Values with Filters in PHP 5

Review: the FILTER_VALIDATE_INT filter - PHP

As you know, when it comes to validating incoming data, PHP 5 comes bundled with a powerful set of native functions that allow you to perform all sorts of clever validations on a given variable. This includes checking for numeric values, arrays, strings, and objects as well. However, the best feature of the helpful validation capabilities offered by default by PHP 5 is its handy filter extension, even though it has been overlooked by many programmers so far.

TABLE OF CONTENTS:
  1. Validating Octal and Hexadecimal Values with Filters in PHP 5
  2. Review: the FILTER_VALIDATE_INT filter
  3. Filtering array elements
  4. Validating octal and hexadecimal integers using the flags option
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 22, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In case you still havenít had an opportunity to read the preceding part of the series, where I discussed the use of the FILTER_VALIDATE_INT filter to validate integers, below I included the examples that I coded then, so you can examine them in detail. Here they are:

(example on validating an integer value)

 

$input = 123456;

// check to see if $input is an integer

echo filter_var($input, FILTER_VALIDATE_INT); // displays 123456

 

 

(example on validating a float value)

 

$input = 1.2;

// check to see if $input is an integer

echo filter_var($input, FILTER_VALIDATE_INT); // displays nothing

 

 

(example on validating a string value)

 

$input = '4.56';

if(filter_var($input, FILTER_VALIDATE_INT) === FALSE) // displays Input is not an integer.

{

echo 'Input is not an integer.';

}

else

{

echo 'Input is an integer.';

}

 

 

(example on validating an integer value)

 

$input = '123456';

if(filter_var($input, FILTER_VALIDATE_INT) === FALSE) // displays Input is an integer.

{

echo 'Input is not an integer.';

}

else

{

echo 'Input is an integer.';

}

 

 

(example on validating an integer value specifying min and max limits)

 

$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';

}

 

 

(example on validating an integer value specifying only max limit)

 

$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';

}

From the code samples listed above, itís clear to see how easy it is to check whether or not the value assigned to a determined variable is an integer by using the FILTER_VALIDATE_INT filter in conjunction with the ďfilter_var()Ē function. In the two last examples, the filter in question has been used along with an additional ďoptionĒ array, which allowed us to specify minimal and maximal boundaries for the integer being validated.

Now that you hopefully grasped the logic that stands behind using the FILTER_VALIDATE_INT filter, itís time to continue exploring the functionality given by the filter extension. In the following section Iím going to explain how to use the FILTER_VALIDATE_INT filter for checking whether or not all the elements of an array are integers.

To see how this functionality of the FILTER_VALIDATE_INT filter will be used in a concrete example, please 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: