Bug #71529 [Opn->Csd]: Variable references on array elements don't work when using count

From: Date: Sat, 06 Feb 2016 15:45:02 +0000
Subject: Bug #71529 [Opn->Csd]: Variable references on array elements don't work when using count
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199079@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71529&edit=1 ID: 71529 Updated by: nikic@php.net Reported by: David dot Gausmann at measX dot com Summary: Variable references on array elements don't work when using count -Status: Open +Status: Closed Type: Bug Package: Scripting Engine problem Operating System: irrelevant PHP Version: 7.0.3 Block user comment: N Private report: N New Comment: Automatic comment on behalf of nikic Revision: http://git.php.net/?p=php-src.git;a=commit;h=9f82f21d018aafbfea723960a335f435202bff77 Log: Fix bug #71529 Previous Comments: ------------------------------------------------------------------------ [2016-02-05 10:20:05] 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-05 08:55:36] 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.) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71529&edit=1

« previous php.bugs (#199079) next »