As I stated in the introduction, the filter extension permits you to validate not only values assigned to a single variable, but to filter elements of an array in one round. In this particular case, I'm going to demonstrate how to perform this task by way of an additional constant called FILTER_REQUIRE_ARRAY, which will be used for validating Boolean values. That being explained, here's another code sample that shows how to use the aforementioned constant in a concrete case. Have a look at it: // example on validating Boolean false values in arrays
$values = array(0, 1, 2, 3, 4, 'FALSE', FALSE, array(0, 1, 2, 3, 4, TRUE, 'TRUE')); $filteredValues = filter_var($values, FILTER_VALIDATE_BOOLEAN, FILTER_REQUIRE_ARRAY); var_dump($filteredValues);
/* displays the following array 0 => boolean false 1 => boolean true 2 => boolean false 3 => boolean false 4 => boolean false 5 => boolean false 6 => boolean false 7 => array 0 => boolean false 1 => boolean true 2 => boolean false 3 => boolean false 4 => boolean false 5 => boolean true 6 => boolean true */ As illustrated by the above hands-on example, validating Boolean values in arrays is a straightforward process that can be grasped very quickly. In this specific situation, the values to check are of multiple types, including string and float numbers, other arrays and so forth, which are first evaluated with the "filter_var()" function in conjunction with the FILTER_REQUIRE_ARRAY constant, and then the corresponding results are displayed on screen via the "var_dump()" PHP function. Well, at this point you should feel pretty satisfied, since you've learned how to check Boolean values in arrays by using the filter extension. Provided that you grasped the logic that drives this process, in the last section of this article I'm going to discuss how to utilize the extension for validating float numbers. As usual, to see how this brand new validation process will be accomplished, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|