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

Validating octal and hexadecimal integers using the flags option - 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.

  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



One handy bonus given by the FILTER_VALIDATE_INT filter is its ability to check integers represented by numeration systems other than the decimal one. This extra functionality can be obtained by specifying a “flags” option when using the “filter_var()” previously explained. By means of these flags, it is possible, for instance, to validate both octal and hexadecimal integers with extreme ease.

The following two examples demonstrate a simple usage of the flags. Look at them, please:



// example on validating hexadecimal integers

// check hexadecimal integers

$hexValue = '0xf0';

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



// example on validating octal integers

$octValue = '7601';

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


As I mentioned before, the “flags” option permits you to check for integer values that have been represented in octal and hexadecimal. In the first case, to validate a hexadecimal value, the FILTER_FLAG_ALLOW_HEX constant has been specified within the “filter_var()” function, while in the second example, the FILTER_FLAG_ALLOW_OCTAL constant has been passed to the function to check an octal integer.

And finally, with these last couple of code samples, I’m finishing this second part of the series that introduces the filter extension that comes included with PHP 5. As usual, feel free to edit all of the examples shown in this tutorial, so you can get a better idea of the numerous validation capabilities offered by this extension.

Final thoughts 

It’s hard to believe, but we’ve come to the end of this second part of this series. Hopefully the experience has been pretty educational, since you learned how to use the FILTER_VALIDATE_INT filter to validate arrays of values, as well as octal and hexadecimal integer values by using the “flag” option.

In the upcoming article, things will become even more interesting. I’m going to explain how to utilize the filter extension to check whether or not a value assigned to a variable is Boolean. So, now that you’ve been warned about the topics that will be covered in the next tutorial, you don’t have any excuse to miss it!

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