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
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: 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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC