php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43072 undesirable invalid argument in foreach
Submitted: 2007-10-22 23:33 UTC Modified: 2007-10-23 09:25 UTC
From: php at bucksvsbytes dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.4 OS: Ubuntu
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at bucksvsbytes dot com
New email:
PHP Version: OS:

 

 [2007-10-22 23:33 UTC] php at bucksvsbytes dot com
Description:
------------
In PHP4, foreach($array) runs without error if $array is an uninitialized array. The foreach block is executed 0 times. This is consistent with other 0 iteration blocks such as while(false) and for($i=0;$i<0). In PHP5, an inconsistency was introduced by throwing an "invalid argument" warning when $array is an uninitialized array. In upgrading to PHP5 I had to add a surrounding if($array) block to hundreds of foreach blocks to eliminate the possible warning message.

Reproduce code:
---------------
unset($array);//just to clarify initial conditions
foreach($array['phone'] as $key=>$phone){
 echo $phone.'<br/>';
}

Expected result:
----------------
I expect no errors and nothing echoed. This is PHP4 behavior.

Actual result:
--------------
In PHP5, following message pointlessly prints:
Warning: Invalid argument supplied for foreach() in ...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-23 09:25 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue May 19 20:00:01 2026 UTC