php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76377 foreach( $a as $a ) works fine ...
Submitted: 2018-05-25 13:50 UTC Modified: 2018-05-25 13:55 UTC
From: nicolas dot aubanel at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.0.30 OS: macos
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: nicolas dot aubanel at gmail dot com
New email:
PHP Version: OS:

 

 [2018-05-25 13:50 UTC] nicolas dot aubanel at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/control-structures.foreach
---

array expression is not reevaluated on each loop

Test script:
---------------
$a = array(1 => array("test1"), 2, 3, 17);
        
foreach ($a as $a) {
    var_dump( $a );
}

Expected result:
----------------
array(1) {
  [0]=>
  string(4) "test1"
}


string(4) "test1"

Actual result:
--------------
array(1) {
  [0]=>
  string(4) "test1"
}
int(2)
int(3)
int(17)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-25 13:55 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-05-25 13:55 UTC] requinix@php.net
That's right, the expression is not reevaluated every time the loop runs. That would create chaos for anything other than simple variables.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC