Bug or feature? (Destructuring of array)

Viewed 36

The destructuring of array with the reference on noexistent element of array = Creation an element in the array.

PHP 8.1.9 (cli) (built: Aug 2 2022 14:17:26) (ZTS Visual C++ 2019 x64) Copyright (c) The PHP Group Zend Engine v4.1.9, Copyright (c) Zend Technologies with Xdebug v3.1.4, Copyright (c) 2002-2022, by Derick Rethans

$arr = [1];

/* > CHECK */

echo 'Before change:';
echo PHP_EOL;

echo 'arr: ';
\print_r($arr);
echo PHP_EOL;

/* < CHECK */

// destructuring
[&$a, &$b] = $arr; // OK

// BUT
//[&$a, $b] = $arr; // Warning: Undefined array key 1

$b = 'insertNewDataInArray';

/* > CHECK */

echo 'After change:';
echo PHP_EOL;

echo 'arr: ';
\print_r($arr);
echo PHP_EOL;
/* < CHECK */

Bug or feature?

0 Answers
Related