note 48561 added to control-structures.foreach
| From: | ajatnospamdotcom at osu1 dot php dot net | Date: | Wed, 29 Dec 2004 16:55:03 +0000 |
| Subject: | note 48561 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-82462@lists.php.net to get a copy of this message | ||
One of the bits of code I find myself using over and over and over is something like this:
if (is_array($array)){
foreach ($array as $index => $value){
// do stuff
}
}
else {
// do other stuff because it isn't an array
}
I use this a lot when I write a class to handle arrays and/or variables. If there is a simpler
solution I would like to know what it is. If not, I have a suggestion the awesome php dev team might
be interested in:
foreach ($array as $index => $value){
// do stuff
}
else {
// do other stuff because it isn't an array
}
How I envision this working is that the else statement runs if $array isn't really an array,
but without the else statement you would get the same error you do now.
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48561
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48561&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48561&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48561&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48561&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48561&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48561&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48561&report=yes
Search -- http://master.php.net/manage/user-notes.php