php’s foreach on a string? you can do that.

tightcenter.smallReminder to self:

Normally, to do a foreach must be something like array(”oliver”,”nassar”). I’ve run into a situation where I didn’t if a variable, lets say $wtf, was an array or just a string. Running foreach on a string results in a notice like “Warning: Invalid argument supplied for foreach() inĀ /random/file/path.php on lineĀ #line-number“.

Just write this, and you should be good:
foreach((array)$unknown as $key=>$value)
echo "go nuts.";
?>

Filed under PHP4, PHP5 · Tagged with , , , ,

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!