

Short array syntax finally in PHP 5.4 - harisenbon
http://svn.php.net/viewvc?view=revision&revision=313641

======
gdulli
My big complaint about arrays wasn't declaring them. It was the endless array
functions you have to use afterwards.

array_push

array_pop

array_slice

array_shift

array_unshift

array_map

array_key_exists

It kind of feels like they missed the point, saving 5 characters when you
declare the array once, ignoring all of the other operations you do
afterwards.

And this RFC, simple as it is, took 3.5 years to come to fruition.

~~~
robryan
Hard to say that it is simple unless you know the internals.

~~~
vmind
2 lines in the parser: [http://svn.php.net/viewvc/php/php-
src/trunk/Zend/zend_langua...](http://svn.php.net/viewvc/php/php-
src/trunk/Zend/zend_language_parser.y?r1=313094&r2=313641)

The RFC (<https://wiki.php.net/rfc/shortsyntaxforarrays>) was actually
rejected by the developers, I think it's only really been put in to appease
large userland pressure. Find it rather amusing that a 2 change to the parser
for an alias would be rejected on 'maintainability' grounds.

~~~
birken
The voting results on the RFC page is from 2008 when it was rejected. The
recent vote that decided its inclusion was here:
<https://wiki.php.net/todo/php54/vote>. I think it had pretty solid support
from core developers as well as userland people this time around.

------
nbpoole
With the removal of bad legacy features like register_globals,
magic_quotes_gpc, and safe_mode combined with new features like array de-
referencing and traits, I'm really excited for PHP 5.4. :)

For anyone who's curious, here's a list of the changes in 5.4 alpha 1:
<http://www.php.net/releases/NEWS_5_4_0_alpha1.txt>

------
barredo
Now if only when a function returns an array we needn't a variable...

    
    
      function boo(){
      return array(1,2,3)
      }
    
      echo boo()[1];

~~~
nbpoole
[http://svn.php.net/viewvc?view=revision&revision=300266](http://svn.php.net/viewvc?view=revision&revision=300266)

<http://www.php.net/archive/2011.php#id2011-06-28-1>

It's in PHP 5.4 alpha as well ;)

~~~
barredo
Thanks!

------
viraptor
Just looked through the RFCs... and I'm a bit surprised noone proposed
unification of types yet. It seems a bit silly that the resources, objects,
arrays, strings are different types at the language level. They could throw
out / reorganise a lot of the randomly named functions this way. Just add the
proper methods to the actual types and sort out the (haystack, needle) order,
underscores or lack of them, etc.

~~~
nyordanov
Right, but wouldn't that make PHP code too good looking? The core devs
wouldn't allow it ;)

My only wish is that they implement something that evaluates {} to
(object)array(). It would come in handy when used with anonymous functions.

Anyway, I am pleased with the new syntax, but sadly, I have to use 5.2 as most
of the time I'm working on WordPress projects...

------
Udo
That's great news, finally we can do

    
    
      $a = [1, 2, 3];
    

and it also will make function calls with named parameters much more readable.

Sadly, it will be years before the majority of hosting providers adopt 5.4...

~~~
program
Also arrays of arrays have a much more compact syntax:

    
    
      $a = [1 => [4, 5, 6], 2 => [7, 8, 9], 3 => [10, 11, 12]];
    
      $a = array(1 => array(4, 5, 6), 2 => array(7, 8, 9), 3 => array(10, 11, 12));

~~~
nknight
Uh, why wouldn't

    
    
      [[4, 5, 6], [7, 8, 9], [10, 11, 12]]
    

work?

I know PHP has a weird history of rebelling against its C-family-syntax
heritage, but requiring the syntax in your example would seem specifically
calculated just to make developers' lives harder.

~~~
joelhaasnoot
Your example would work, but there's a reason you might want different keys.
My company's current web app is written in CakePHP, which has a love for
arrays. Some of my queries look like this:

    
    
      $booking = $this->Booking->find('first', array('conditions' => array('Booking.booking_id' => $id), 'contain' => array('Review', 'Desk' => array('fields' =>
     array('provider_id', 'category_id'), 'Provider' => array('city', 'name'), 'ProviderCategory' => array('fields'   => array('provider_id', 'category_id'), 'DeskCategory' => 'name')))));
    

It's essentially not readable, but with [] it would work much better, and I
still want to give keys to arrays.

~~~
josegonzalez
As an FYI, you might want to look into custom find methods if you haven't
already:
[https://github.com/josegonzalez/documentation/blob/master/03...](https://github.com/josegonzalez/documentation/blob/master/03-good-
cake/01-models.textile)

------
Tharkun
Still strikes me as a useless bit of syntactic sugar. Is it _really_ that much
work to write array('foo' => 'bar')? As far as I can tell, writing ['foo' =>
'bar'] isn't that much shorter in the first place.

 _sigh_ Can the PHP devs focus on the real problems now? _cough_ Unicode
anyone? Ridiculously inconsistent naming of string and array functions? The
woefully incomplete and useless STL? The lack of OO in arrays and strings?

~~~
MostAwesomeDude
Hey, I've been complaining about PHP's lack of Unicode support forever. Every
time I do, though, an apologist comes out of the woodwork to tell me about the
mb_*() functions. Apparently the PHP community is quite comfortable not caring
about non-ASCII characters.

~~~
nosnih
[http://www.slideshare.net/andreizm/the-good-the-bad-and-
the-...](http://www.slideshare.net/andreizm/the-good-the-bad-and-the-ugly-
what-happened-to-unicode-and-php-6)

Andrei Zmievski has been giving this presentation on what happened to Unicode
in PHP. A huge amount of work went into making it happen but it eventually
could not continue due to several reasons, including design decisions, lack of
contributors & interest in the project.

------
snorkel
My complaint about arrays in PHP is warnings for reading a array member that
may not exist, such as when reading a form input that might not be there.

    
    
        $foo = $_POST["foo"];
    

Undefined array key! Oh, no! Tragedy! A crime has been committed! Are you
kidding? Just set $foo to undefined and let me deal with the consequences,
please?

~~~
code_duck
You really wouldn't like Python or even stricter languages such as Scala or
java, then. In Python, an attempt to access a nonexistent dictionary key will
throw an exception which halts execution if not caught. (similar to calling a
nonexistent method or function in PHP, but since PHP doesn't have a well-
thought out exception system/std. lib as Python does, you can't catch anything
in that situation in PHP).

~~~
cheald
In Python, if you're expecting a potentially missing key, you can just use
dict.get('key') rather than dict['key']. The equivalent in PHP would be
something like:

    
    
      $val = array_key_exists('key', $dict) ? $dict['key'] : NULL;
    

Being able to say "I expect a potentially nil value (and am okay with that)"
via get() rather than array index is both semantically useful, and far more
beautiful than the PHP equivalent.

~~~
code_duck
Sure, or you can catch a KeyError and do something else entirely. I'm aware of
all this, just pointing out some differences to the person to whom I replied.

Python's dict.get is a bit like the getOrElse found in Scala. It's quite handy
and can be seen as advanced conceptually.

I actually added some code like that to a recent post about PHP here:
<http://news.ycombinator.com/item?id=2771304>

------
MattBearman
That's good to see, but like many have said, I'd really like to see proper OO
stuff like $myArray->pop();

What about short object declaration syntax:

$obj = { param:'blah' };

PHP already has stdClass, so I doubt it would take much of a parser change to
implement that.

------
thesmartace
While I don't use PHP any more, this is still a massive improvement for those
that do. Next on the list: Array and String literals as proper objects!

------
schiptsov
"Perfection is achieved not when there is nothing more to add, but when there
is nothing more to cut off."

Adding fancy syntax does not magically transform that crapware to something
almost perfect like python3 or ruby (in its way) where these syntactic forms
are among foundations of the language and are supported through all basic
idioms. ^_^

------
snomad
Looks like they decided NOT to include the scalar type hint.
<https://wiki.php.net/todo/php54>

Even better would be bool/float/int/string/func ref type hinting...

------
agotterer
What's the release roadmap and dates for 5.4?

~~~
nbpoole
<https://wiki.php.net/todo/php54>

------
Luyt
$I $wish $they $would $get $rid $of $prefixing $every $variable $with $a
$dollar $sign.

------
jrockway
So does => quote the lhs, like Perl? Otherwise, what's the point?

~~~
ars
I take it you don't know anything about PHP?

------
ck2
But PHP isn't a transmitted language like javascript, why even bother? It will
only make the parser slower?

~~~
jey
They're trying to be nicer to the humans. (Though IMO, any human who wanted to
be kind to himself would just not use PHP to begin wtih.)

~~~
ma2rten
Right now those humans would not choose to start a project in php, but a few
years ago there were not too much alternatives. It's not for nothing that the
world's second biggest website is written in php.

~~~
code_duck
Do you mean Facebook? They only use PHP for templating and legacy purposes, as
far as I understand. Much of their newer code is written in a variety of
languages, including C++, Python and Erlang.

