php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81228 print_r incorrectly linked to pass by reference loop
Submitted: 2021-07-06 19:25 UTC Modified: 2021-07-06 20:59 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: manuel at m-arzberger dot de Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: 7.4.21 OS: Ubuntu 20.04
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: manuel at m-arzberger dot de
New email:
PHP Version: OS:

 

 [2021-07-06 19:25 UTC] manuel at m-arzberger dot de
Description:
------------
I'm using PHP 7.4.21 on Ubuntu 20.04

When you loop through an array the first time with pass by reference and then do it again without passing by reference, print_r will return unexpected values. The last item of the array will not be displayed, but the second to last.

The array shouldn't be linked in any way to the pass be reference loop at the point.

Test script:
---------------
<?php

$array = array('one', 'two', 'three', 'four');

foreach($array as &$value) { }
foreach($array as $value) { }

print_r($array);

Expected result:
----------------
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)

Actual result:
--------------
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => three
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-06 20:59 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-07-06 20:59 UTC] cmb@php.net
See the warning box in the docs: <https://www.php.net/manual/en/control-structures.foreach.php>.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon May 23 21:03:42 2022 UTC