php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71529 Variable references on array elements don't work when using count
Submitted: 2016-02-05 08:55 UTC Modified: -
From: David dot Gausmann at measX dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.3 OS: irrelevant
Private report: No CVE-ID: None
 [2016-02-05 08:55 UTC] David dot Gausmann at measX dot com
Description:
------------
In a websites code we have got nested arrays for a tree structure.
In PHP 5.5 that code worked fine, but since PHP 7 it doesn't work anymore.
I've read the "backward incompatibility changes", but this point seems not to be listed there (and in my opinion it is a bug).

In my code I am pushing variables by-reference into an stack array. The references are referencing to the last element of another entry of the stack array.
The bug seems to depend on the usage of count(). If I use fixed indices for testing purposes, then the code works fine.

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

$avOutput = array('test' => array());

$avStack = array(&$avOutput['test']);

$avTemp = array('data' => 1, 'test' => array());
$avStack[count($avStack) - 1][] = $avTemp;
$avStack[] = &$avStack[count($avStack) - 1][count($avStack[count($avStack) - 1]) - 1]['test'];		// WORKS NOT in PHP7 (but worked in PHP 5.5)
//$avStack[] = &$avStack[0][0]['test'];			// WORKS
$avTemp = array('data' => 2, 'test' => array());
$avStack[count($avStack) - 1][] = $avTemp;

echo "<xmp>";
print_r($avOutput);
echo "</xmp>";

?>

Expected result:
----------------
Array ( [test] => Array ( [0] => Array ( [data] => 1 [test] => Array ( [0] => Array ( [data] => 2 [test] => Array ( ) ) ) ) ) )

(You can see that [data] => 1 and [data] => 2 are both listed. This was the result in PHP 5.5)

Actual result:
--------------
Array ( [test] => Array ( [0] => Array ( [data] => 1 [test] => Array ( ) ) ) )

(You can see that [data] => 1 is there, but [data] => 2 is missing. If you uncomment the line with fixed indices, you become the correct result. If you output the result of the both count(), you will get 0 for each, so both lines should be equal.)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-05 10:20 UTC] inefedor at gmail dot com
Here's a smaller reproducing script https://3v4l.org/q2mmT

PHP 7 has some changes in the way how references work, they may influence such non obvious changes in behavior. I would recommend you using objects instead of arrays with references, that way this code would also be easier to read and test.
 [2016-02-06 15:45 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=9f82f21d018aafbfea723960a335f435202bff77
Log: Fix bug #71529
 [2016-02-06 15:45 UTC] nikic@php.net
-Status: Open +Status: Closed
 [2016-02-06 15:58 UTC] nikic@php.net
The underlying issue is not fully resolved yet. Followup bug: https://bugs.php.net/bug.php?id=71539
 [2016-07-20 11:33 UTC] davey@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=9f82f21d018aafbfea723960a335f435202bff77
Log: Fix bug #71529
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC