PHP has the language building list()
, which is used to unstructure values from within an array. That is, with list()
, you can unpack values from within an array without using the index or key after the array.
Examples:
<?php
$userInfoPair = array("John Doe", 50);
list($name, $age) = $userInfoPair;
var_dump($name); //=> string(8) "John Doe"
var_dump($age); //=> int(50)
Or, if the array values are determined by keys:
<?php
$userInfo = array(
"name" => "John Doe",
"age" => 50
);
list("name" => $name, "age" => $age) = $userInfo;
var_dump($name); //=> string(8) "John Doe"
var_dump($age); //=> int(50)
It can then be verified that, as well as array()
, the list()
is not, in fact, a function, but rather a language building.
And so is the array()
, which received the new bracket syntax, the list()
also. Thus, I do not believe that there is a difference between the respective new syntaxes (with brackets), since they do exactly the same thing. A own documentation states that it is only a shorthand syntax as a alternative to the syntax of list()
.
The "problem" they must have seen to justify the introduction of these new syntaxes was to keep typing array()
or list()
when one wanted to build or destroy an array. Also (my guess) could easily be confused with functions, which is not technically true.
So, since nothing changes "internally", it is a mere matter of aesthetics. There is no denying that the syntax is much more succinct:
Examples (corresponding to the two above, in order):
<?php
$userInfoPair = ["John Doe", 50];
[$name, $age] = $userInfoPair;
var_dump($name); //=> string(8) "John Doe"
var_dump($age); //=> int(50)
And:
<?php
$userInfo = [
"name" => "John Doe",
"age" => 50
];
["name" => $name, "age" => $age] = $userInfo;
var_dump($name); //=> string(8) "John Doe"
var_dump($age); //=> int(50)
list
can become deprecated with this new implementation?
It’s the same thing as asking if the building array()
can become deprecated. How much legacy code depends on these constructions, think that no. However, I cannot state with complete certainty.