====== PHP RFC: Short Array Variable Shorthand ====== * Version: 1.0 * Date: 2026-09-30 * Author: Weilin Du, weilindu@php.net * Status: Under Discussion * Implementation: TBD * Discussion thread: TBD * Voting thread: TBD ===== Introduction ===== Associative arrays often repeat a key and a variable with the same name. This RFC proposes '':$name'' as shorthand for ''"name" => $name'' inside short ''[]'' syntax: $x, 'y' => $y] [:$x, :$y] = $point; // ['x' => $x, 'y' => $y] = $point ?> ===== Proposal ===== Within ''[]'', '':$name'' uses the variable's lexical name as a string key. In an array expression it reads ''$name''; in destructuring it assigns the value at key ''"name"'' to ''$name''. Both forms behave exactly like the explicit keyed element, including existing rules for duplicate or missing keys. The shorthand also works in nested ''[]'' and in ''[]'' destructuring used by ''foreach''. It accepts only a simple variable. Other values still use an explicit key, for example ''["name" => $object->name]''. Important: This RFC supports only ''[]''. The forms ''array(:$name)'' and ''list(:$name)'' remain invalid. It does not change function calls. ==== Examples ==== An array can mix the shorthand with explicit keys: 1, :$name, :$age]; // ['id' => 1, 'name' => 'Weilin', 'age' => 18] ?> The shorthand can also be mixed with explicit keys when destructuring: 'Weilin', 'age' => 18]; [:$name, 'age' => $years] = $user; // $name === 'Weilin'; $years === 18 ?> Here is an example in a foreach loop: 'Weilin']]; foreach ($rows as [:$name]) { echo $name; // Weilin } ?> Nested short-array destructuring works in the same way: ['name' => 'Weilin']]; ['user' => [:$name]] = $record; echo $name; // Weilin ?> ===== Backward Incompatible Changes ===== None. The proposed syntax is currently invalid. ===== Proposed PHP Version(s) ===== PHP 8.7. ===== RFC Impact ===== ==== To the Ecosystem ==== Static analyzers must recognize the new ''[]'' element form. ==== To Existing Extensions ==== None. ==== To SAPIs ==== None. ===== Open Issues ===== None. ===== Future Scope ===== The long ''array()'' and ''list()'' forms, and shorthand in function calls, are outside this RFC. ===== Voting Choices ===== TBD. Not in the voting process. ===== Patches and Tests ===== TBD. ===== Implementation ===== TBD. ===== References ===== * [[https://wiki.php.net/rfc/named_params#shorthand_syntax_for_matching_parameter_and_variable_name|Named Arguments RFC: Future Scope]] * [[https://www.php.net/manual/en/language.types.array.php#language.types.array.destructuring|PHP manual: array destructuring]] ===== Rejected Features ===== None. ===== Changelog ===== * 2026-09-30: Initial draft. * 2026-09-30: Added examples and changed status to Under Discussion.