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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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: Wed Apr 24 17:01:30 2024 UTC