php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #3462 foreach behavior on a non-array
Submitted: 2000-02-11 22:38 UTC Modified: 2002-07-27 07:27 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: leon at leonatkinson dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.0 Beta 3 OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2000-02-11 22:38 UTC] leon at leonatkinson dot com
Given the following code:

unset($list);
foreach($list as $value)
{
   print($value);
}

and $list is FALSE, PHP is generating an error like
"Non array argument supplied for foreach()".  The
variable probably should be promoted to being an
empty array, but I can deal with casting it myself.
Unfortunately, when I do this:

unset($list);
foreach((array)$list as $value)
{
   print($value);
}

it runs through the loop once.  I know I can get around
this using a while loop, but I'd prefer to use foreach.

If anyone's wondering why the hell I'm looping over an
unset variable, I have a caching function that returns
the contents of a database table.  When there are no
rows, it returns the unset static variable.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-07-31 22:41 UTC] waldschrott@php.net
Why don?t you let your cache function return an empty array?
I think you should take care about passing foreach() the
right types...

Anyway PHPs handling of conversion unfortunately differs a
lot from function to function and statement to statement.
Thus I really don?t know what to do with this one I?ll mark
it analyzed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC