Re: SUMMARY: Array syntax
| From: | Kouber Saparev | Date: | Mon, 14 Jan 2008 15:27:11 +0000 |
| Subject: | Re: SUMMARY: Array syntax | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-34783@lists.php.net to get a copy of this message | ||
A +1 from me (in case my vote counts).
The main reason behind it is readability:
$x = array(1 => array(1, 2, 3), array(array(1), 2));
vs.
$x = [1 => [1, 2, 3], [[1], 2]];
The latter form looks much better to me - it is faster to read and to understand it (and to type it, of course, although it shouldn't be a factor), especially for such multi-dimensional definitions.
The benefit of using the new syntax is even bigger when dealing with function calls + conditionals:
if (in_array($z, array(1, 2, 3))) {
$s = str_replace(array('a', 'b', 'c'), array('d', 'e', 'f'), 'abc');
}
vs.
if (in_array($z, [1, 2, 3])) {
$s = str_replace(['a', 'b', 'c'], ['d', 'e', 'f'], 'abc');
}
With the "square brackets" syntax a pair of redundant parantheses is removed, thus making it easier to distinguish between data definition and function call (conditional, expression...) closures and helping identify arrays among such compound structures. ;-)
Regards,
--
Kouber Saparev
http://kouber.saparev.com