Live data from Hacker News

Short array syntax finally in PHP 5.4

svn.php.net

11–20 of 80 posts

Re: Short array syntax finally in PHP 5.4

#11
post #7

Now if only when a function returns an array we needn't a variable... function boo(){ return array(1,2,3) } echo boo()[1];

It's not quite what you're asking for, but there is a way to handle array results from functions where you know the number of elements that will be returned.

For example:

// explode splits the string on the provided boundary

// and returns an array containing each segment.

// using list() we can assign the two segments to two

// variables within the same statement

$size = "1000x1000";

list( $width, $height ) = explode( "x", $size );

Re: Short array syntax finally in PHP 5.4

#12
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

Re: Short array syntax finally in PHP 5.4

#13
post #10
post #7

Now if only when a function returns an array we needn't a variable... function boo(){ return array(1,2,3) } echo boo()[1];

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 ;)

Thanks!

Re: Short array syntax finally in PHP 5.4

#14
post #7

Now if only when a function returns an array we needn't a variable... function boo(){ return array(1,2,3) } echo boo()[1];

It's not quite what you're asking for, but there is a way to handle array results from functions where you know the number of elements that will be returned. For example: // explode splits the string on the provided boundary // and returns an array containing each segment. // using list() we can assign the two segments to two // variables within the same statement $size = "1000x1000"; list( $width, $height ) = explod…

Ah yes, I love nothing more than functions on the left side of assignment.

Edit: Yes, it is a special form, but its still irregular, and that makes it shitty.

Re: Short array syntax finally in PHP 5.4

#15

Earlier quoted context omitted.

It's not quite what you're asking for, but there is a way to handle array results from functions where you know the number of elements that will be returned. For example: // explode splits the string on the provided boundary // and returns an array containing each segment. // using list() we can assign the two segments to two // variables within the same statement $size = "1000x1000"; list( $width, $height ) = explod…

Ah yes, I love nothing more than functions on the left side of assignment. Edit: Yes, it is a special form, but its still irregular, and that makes it shitty.

http://www.php.net/manual/en/function.list.php

It's a language construct, not a function.

Re: Short array syntax finally in PHP 5.4

#16
post #6
post #4

Earlier quoted context omitted.

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.

Well, arrays start with index 0, so if the indexes where 1 less it would be the same. Your example would be equivalent to [0 => [4, 5, 6], 1 => [7, 8, 9], 2 => [10, 11, 12]]

In addition, you only need to specify the first key to create a one-based array. Indexing will carry on from there.

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

Re: Short array syntax finally in PHP 5.4

#18
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.

Post reply on HN