ID:               19140
 Comment by:       phil-nospam-ippe at morange dot net
 Reported By:      jc at mega-bucks dot co dot jp
 Status:           Bogus
 Bug Type:         Arrays related
 Operating System: Red Hat Linux 7.2
 PHP Version:      4.2.2
 New Comment:

I agree that it would be a great thing if foreach() could manage empty
or uninitialized arrays without throwing any error...

Because in my script I have to replace each occurence of this:

foreach ($a as $val) {
    ...
}

with :

if (is_array($a)) {
    foreach ($a as $val) {
        ...
    }
}

which is quite a hassle to do.



Take a look at the following example:

$a = '';

while ($a) {
    //...
}

foreach ($a AS $val) {
    //...
}

Why doesn't the "while" throw an error if the "foreach" does???

I guess it wouldn't be much to change in PHP source code???


Previous Comments:
------------------------------------------------------------------------

[2002-08-29 03:50:23] jc at mega-bucks dot co dot jp

Sorry about that. Damned form manager ...

------------------------------------------------------------------------

[2002-08-28 12:34:01] [EMAIL PROTECTED]

fix the summary..


------------------------------------------------------------------------

[2002-08-28 05:58:56] jc at mega-bucks dot co dot jp

Super! Thanks.

Is there any chance that in the future foreach will be able to handle
null arrays. I'm used to foreach in perl and if I remember correctly it
doesn't mind unitialized arrays. But I might be wrong ...

Anyway, I'm more than happy with a clarification in the docs.

------------------------------------------------------------------------

[2002-08-28 05:41:03] [EMAIL PROTECTED]

FYI: I clearified this in the manual, it will show up in a few days
online.

Derick

------------------------------------------------------------------------

[2002-08-28 05:32:48] jc at mega-bucks dot co dot jp

Neglectful coding? Why do you consider it neglectful coding if I do
this for an array but good coding for a string? PHP treats null strings
as empty strings, why doesn't it do the same for arrays?

All I want to know is, what is the documented behaviour for foreach on
a null array. If there is no documented behaviour, let's add to the
documentation. Something like:

"foreach will fail on an unitialized (null) array".

I had assumed that since PHP is usually treats null strings as empty
strings it would do the same with null arrays. I was wrong. I tried to
find documentation on what foreach will do in the case of a null array
and could not find any.

I agree with you that this is neglectfull coding. It would be nice if
there was documentation to back it up :)

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/19140

-- 
Edit this bug report at http://bugs.php.net/?id=19140&edit=1

Reply via email to