arrays - PHP Invalid argument supplied for foreach() -
i have array:
$y_exceptions = array( "lay", "interlay", "display", "delay", "obey", "decay", "play", "slay" );
i'm checking if word in array this:
foreach($y_exceptions $thisexception) {
which throws error
invalid argument supplied foreach()
what have done wrong?
$y_exceptions
needs array processed foreach loop. possible $y_exceptions
set null
before foreach loop. null
not treated array, foreach expects—hence warning.
on solution stricter validation before foreach:
if (!is_array($y_exceptions)) { // throw exception }
alternatively, instruct foreach loop run $y_exceptions
array, though may cause errors downstream:
foreach ((array) $y_exceptions $thisexception) { // stuff }
Comments
Post a Comment