HomePHP 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.
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:
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.
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!