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: <?php
// 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 thoughtsIt’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!
blog comments powered by Disqus |
|
|
|
|
|
|
|