When we are working with loops, we sometimes need to check first if the variables are iterable. Otherwise, we get the warning message “Warning: Invalid argument supplier for foreach()” in PHP. We get this warning when we use variables are of invalid data type, .e.g., non-iterable.
If you’re creating an application from ground up, you may already have coded or even designed the core components. Some of them check for and avoid non-traversable parameters in some methods or functions. These functions use these variables in foreach statements within them. However, if you are modifying an existing and unfamiliar application you would want to keep the changes localized to avoid affecting other parts of the program. In the case for foreach, a simple check of the arguments can be performed on them before passing to the looping construct.
This article demonstrates how to perform checking on a variable to determine whether it is traversable to not before letting foreach use it.
Software Requirements
- Windows 11
- PHP 5.5.9 / Zend Engine v2.5.0
- PHP 7, PHP 8
- NetBeans IDE 8.0.1
PHP 5.5.x foreach Codes to Avoid Invalid Argument
Consider the follow code example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | /* * Copyright 2014 www.turreta.com. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Description of ForEachInvalidArgumentFixDemp * * @author www.turreta.com */ class ForEachInvalidArgumentFixDemp { public $last_name = 'Smith'; public $first_name = 'John'; public $middle_name = 'Head'; public function do_stuff() { } } /** * Determine if a variable is iterable. i.e. can be used to loop over. * * @return bool */ function is_iterable($var) { $bIsIteratble = false; if($var !== null && (is_array($var) || $var instanceof Traversable || $var instanceof Iterator || $var instanceof IteratorAggregate || (is_object($var) && count(get_object_vars($var)) > 0 ))) { $bIsIteratble = true; } return $bIsIteratble; } var_dump(is_iterable('turreta.com')); var_dump(is_iterable(123)); var_dump(is_iterable(['tu','rre', 'ta', '.com'])); var_dump(is_iterable(new SplDoublyLinkedList())); var_dump(is_iterable(new ArrayIterator())); var_dump(is_iterable(new ForEachInvalidArgumentFixDemp())); // Sample usage with foreach $oSample = new ForEachInvalidArgumentFixDemp(); if(is_iterable($oSample)) { foreach($oSample as $key => $val) { echo $key . " = " . $val . "\n"; } } |
PHP 7 (PHP 7 >= 7.1.0, PHP 8) built-in is_iterable Function with foreach
When we are using PHP 7 and greater, we could use the built-in is_iteratable function to check if the variables are of valid type before using them in a foreach() statements. Therefore, there is no need to use the is_iterable function on this post for PHP 7 (and greater) when working with foreach().