A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://wiki.php.net/rfc/shortsyntaxforarrays below:

PHP: rfc:shortsyntaxforarrays

rfc:shortsyntaxforarrays

2008 Votes

This RFC will discuss an language enhancement for simple and easy array definition.

Update: May 2011

This topic has come up again on the list.

Introduction

In some cases it is necessary that you have to pass function parameters as an array. Especially when you work with nested arrays the php array function tends to make the code looking ugly. If you take a look at other languages (Ruby, Python, Javascript) there is a short syntax for arrays using square brackets. Maybe PHP should adopt this behaviour to make code more readable and maintainable.

Another solution would be to implement named parameters.

Syntax

Ryusuke suggested two possibly syntaxes and also provides two patches for a solution:

Square bracket array shortcut - keys and values are separated by colons:

    $a = [1, 2, 3];
    $b = ['foo': 'orange', 'bar': 'apple', 'baz': 'lemon'];

Square bracket array shortcut - keys and values are separated by double arrows:

    $a = [1, 2, 3];
    $b = ['foo' => 'orange', 'bar' => 'apple', 'baz' => 'lemon'];
Proposal and Patch

The first solution is more compact: http://www.opendogs.org/pub/php-5.3dev-080109-sbar.patch
The second solution is more “The PHP Way”: http://www.opendogs.org/pub/php-5.3dev-080109-sbar2.patch

Pro / Contra

Pro and contra arguments that were derived from the list.

Pro Contra Conclusion

This patch has been merged in PHP 5.4.0 and trunk.

Discussion on the List

rfc/shortsyntaxforarrays.txt · Last modified: 2025/04/03 13:08 by 127.0.0.1



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3