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.
blog comments powered by Disqus |
|
|
|
|
|
|
|