|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-12-04 16:41 UTC] filippo dot desantis at gmail dot com
Description: ------------ I'm using PHP 7.0.0 (cli) (built: Dec 2 2015 13:35:31) Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies Running the example in the RFC https://wiki.php.net/rfc/abstract_syntax_tree for the list() function $a = [1, 2]; list($a, $b) = $a; // OLD: $a = 1, $b = 2 // NEW: $a = 1, $b = null + "Undefined index 1" I don't get the "NEW" result but the "OLD" one. Looking for some more information I found in the documentation (http://php.net/list) the warning "Modification of the array during list() execution (e.g. using list($a, $b) = $b) results in undefined behavior.". I was wondering which of the two behaviour should be the good one :) Test script: --------------- <?php $a = [1, 2]; list($a, $b) = $a; var_dump($a, $b); PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 21:00:01 2025 UTC |
While I don't really care about how this interacts with variable variables, I do think that @bwoebi's reference example should work. I tried to fix this by always generating a QM_ASSIGN instead of only for known self-assignment. Unfortunately this broke some existing tests, because apparently we allow passing the result of a list() assignment by reference if the RHS is a CV. This of course breaks if there's a QM_ASSIGN in between. This is already a problem now for the self assign case: <?php function change(&$ref) { $ref = [1, 2, 3]; } // Works $array = [1]; change(list($val) = $array); var_dump($array); // Notice: Only variables should be passed by reference $array = [[1]]; change(list($array) = $array); var_dump($array); I think I'm going to go ahead with this change anyway, because it clearly only works by accident. E.g. if the RHS happens to be anything but a CV, you'll get a fatal error: // Fatal error: Only variables can be passed by reference $array = [[1]]; change(list($val) = $array[1]); var_dump($array); Since PHP 7.3, it is possible to pass the result directly by-ref if a by-ref list() assignment is used, in which case this will always be well-defined.