In case you haven’t read the previous article of the series, where I explained how to use the filter PHP 5 extension for checking integer values in arrays and their respective representations in the octal and hexadecimal systems, here are the pertinent code samples that demonstrate how to accomplish this task by using the FILTER_VALIDATE_INT filter. Take a look at them, please: (example on using the filter_var_array() function with FILTER_VALIDATE_INT filter)
<?php
$values = array(1, '0.675', -123456, 'This is a string', "abcdef", array(1, 2, 3)); // create an array of filtered values $filtered_values = filter_var_array($values, FILTER_VALIDATE_INT); foreach($filtered_values as $key=>$value) { echo '<p>' . $key .' ---- ' . $value.'</p>'; }
/* displays the following 0 ---- 1
1 ----
2 ---- -123456
3 ----
4 ----
5 ---- Array
*/ ?>
(example on validating hexadecimal integers)
<?php
// check hexadecimal integers $hexValue = '0xf0'; echo filter_var($hexValue, FILTER_VALIDATE_INT, array('flags' => FILTER_FLAG_ALLOW_HEX)); // displays 240 ?>
(example on validating hexadecimal integers)
<?php
// checking octal integers $octValue = '7601'; echo filter_var($octValue, FILTER_VALIDATE_INT, array('flags' => FILTER_FLAG_ALLOW_OCTAL)); // displays 7601 ?> Undeniably, the above examples are very easy to follow, meaning that you shouldn’t have major problems understanding how they work. The first case illustrates a simple usage of the handy “filter_var_array()” function along with the FILTER_VALIDATE_INT filter to check whether or not the elements of a given array are integers, while the other two examples show how to use the same filter to check integers represented in hexadecimal and octal systems respectively. Well, now that you've surely grasped the logic that stands behind using the FILTER_VALIDATE_INT filter, it’s time to continue exploring the numerous capabilities offered by the filter extension. With that idea in mind, in the course of the following section I’m going to discuss the usage of yet another helpful filter, which will come in handy for validating Boolean values. As I said before, this particular topic will be discussed in depth in the following segment. So, if you want to learn more about it, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|