php / doc-en

English PHP documentation
505 stars 736 forks source link

Destructuring assignment from null does not raise warning #3640

Open tgr opened 3 months ago

tgr commented 3 months ago

https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax.destructuring says

Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_WARNING-level error message (E_NOTICE-level prior to PHP 8.0.0) will be issued, and the result will be null.

but this isn't actually true when assigning null; [$x, $y] = null; does not raise a notice or warning (eval). Same goes for destructuring with an associative array (eval). This seems to be specific to null (e.g. [$x, $y] = []; raises a warning: eval) and to destructuring assignment (e.g. null[0] raises a warning: eval).

Not sure if this is a bug or a documentation issue, but either there should be a warning or this special case should be documented.

cmb69 commented 3 months ago

Well, let's say it's a feature, but document it (also for list()).