php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19140 foreach fails on empty or unitialized variable
Submitted: 2002-08-28 04:37 UTC Modified: 2012-09-01 15:51 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: jc at mega-bucks dot co dot jp Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.2.2 OS: Red Hat Linux 7.2
Private report: No CVE-ID: None
 [2002-08-28 04:37 UTC] jc at mega-bucks dot co dot jp
The following code generates an error/warning:

$a = return_nothing();
  
foreach ($a as $val) {
  echo "hi";
}

function return_nothing() {
}

OUTPUT:

Warning: Invalid argument supplied for foreach() in test.php on line 5

The documentation does not state what the correct/expected behaviour of foreach should be in the case of an unitialized array. It would be nice if foreach could recognize unitialized variables as being equivalent to "empty" arrays ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-28 04:41 UTC] cynic@php.net
"an uninitialized array"? i don't see anything like that in your example.
there's just one nonexistent (null, if you must) variable. no array.

 [2002-08-28 04:48 UTC] jc at mega-bucks dot co dot jp
$a is an unitialized variable. It is not an empty variable ($a = "").

It is declared but not initialized. It has no value since the function returns nothing.

The problem is that PHP cannot tell that $a is supposed to be an array since $a has no value or content whatsoever.
 [2002-08-28 05:11 UTC] jc at mega-bucks dot co dot jp
What is the documented/expected behaviour for foreach on "null" arrays?

PHP has not problems with statements such as:

echo "$a";

So I don't see why it should have a problem with:

foreach ($a as $val) {};

Actaully I *do* understand why it is a problem :) I just would like it to suggest that foreach be able to handle null arrays. It would be *such* a nice feature ;)
 [2002-08-28 05:17 UTC] cynic@php.net
if you insist on neglectful coding, lower your error_reporting level.

 [2002-08-28 05:32 UTC] 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 :)
 [2002-08-28 05:41 UTC] derick@php.net
FYI: I clearified this in the manual, it will show up in a few days online.

Derick
 [2012-09-01 15:45 UTC] mail at waclawjacek dot com
I second jc. It would be nice not to have to do if ( is_array() ) before every 
foreach(), like you don't have to with while(), when the variable is null.
 [2012-09-01 15:51 UTC] rasmus@php.net
Often that points to a bug. If you are ok with it, just cast it. No need for an 
is_array() check. eg.

foreach( (array)$a as $val ) ...
 [2012-09-01 15:54 UTC] mail at waclawjacek dot com
I found this idea somewhere on the net, thanks, it's pretty neat, still, it was a 
surprise to find out this is how foreach() behaves after years of using while() 
to loop through an array of database results.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Mar 28 19:01:27 2020 UTC