php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31194 foreach without "$key =>" is not compatible with pre-versions
Submitted: 2004-12-20 10:41 UTC Modified: 2004-12-20 12:44 UTC
From: yubing at cernet dot edu dot cn Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.10 OS: Redhat Linux 8.0
Private report: No CVE-ID: None
 [2004-12-20 10:41 UTC] yubing at cernet dot edu dot cn
Description:
------------
foreach on array containing objects without "$key =>" is not compatible with pre-versions

Is that because of the porting of those code from v5.0.2 ?

b2evo and xoops use many this kind of statements to fetch the database objects.

Reproduce code:
---------------
$a = array();
$user->name = "peter";
$user->age = "24";
$a[0] = $user;
$user->name = "ice";
$user->age = "25";
$a[1] = $user;

    foreach ( $a as $u){
    // foreach ( $a as $key => $u) works ok
    print $u->name.": ".$u->age."\n";
        var_dump($u);
    }


Expected result:
----------------
peter:24
ice:25
object(stdClass)(2) {
  ["name"]=>
  string(5) "peter"
  ["age"]=>
  string(2) "24"
}
object(stdClass)(2) {
  ["name"]=>
  string(3) "ice"
  ["age"]=>
  string(2) "25"
}

Actual result:
--------------
NULL:NULL
NULL:NULL
array(2) {
  [0]=>
  object(stdClass)(2) {
    ["name"]=>
    string(5) "peter"
    ["age"]=>
    string(2) "24"
  }
  [1]=>
  int(0)
}
array(2) {
  [0]=>
  object(stdClass)(2) {
    ["name"]=>
    string(3) "ice"
    ["age"]=>
    string(2) "25"
  }
  [1]=>
  int(1)
}

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-20 10:51 UTC] yubing at cernet dot edu dot cn
faint, maybe it's because of the problem of ZendOptmizer.
 [2004-12-20 11:26 UTC] derick@php.net
Do not file bugs when you have Zend extensions (zend_extension=)
loaded. Examples are Zend Optimizer, Zend Debugger, Turck MM Cache,
APC, Xdebug and ionCube loader.  These extensions often modify engine
behavior which is not related to PHP itself.

.
 [2004-12-20 12:44 UTC] yubing at cernet dot edu dot cn
confirmed :P Zend Optimizer 2.5.7 works well
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 18:01:29 2024 UTC