[PHP-DEV] [RFC proposal] Syntactic sugar for array push()

A proposal to add syntactic sugar for array_push() in PHP.

Syntax:
// now

array_push($array, 1);

// new syntax

$array[+]=1;

Dušan Kreheľ

But we already have $array = 1; - are you unaware of it or is there some other benefit to what you are suggesting?

What might interest people would be a syntactic sugar/shorthand for array_pop but I have no idea what that might look like.

···

http://about.me/kenguest/

Hm, I didn’t know about $array = 1;.

It would make sense if it were: $a[+] for push() and $a[-] for pop().

pi 12. 9. 2025 o 12:35 Ken Guest <kguest@php.net> napísal(a):

···

http://about.me/kenguest/

array_push would only be used if there were a [-] in the code, i.e. a hyphen (or plus for pop()) as pseudo-constant.

Practically, it would change today’s behavior that returns an error into a correct operation:

<?php $a=[3]; $a[+]=2; // PHP Parse error: syntax error, unexpected token "]" in … on line 3 pi 12. 9. 2025 o 12:19 Dušan Kreheľ <[dusankrehel@gmail.com](mailto:dusankrehel@gmail.com)> napísal(a): > A proposal to add syntactic sugar for array_push() in PHP. > > Syntax: > // now > > array_push($array, 1); > > // new syntax > > $array[+]=1; > > Dušan Kreheľ

On Fri, Sep 12, 2025, at 13:06, Dušan Kreheľ wrote:

Hm, I didn’t know about $array = 1;.

It would make sense if it were: $a[+] for push() and $a[-] for pop().

pi 12. 9. 2025 o 12:35 Ken Guest <kguest@php.net> napísal(a):

But we already have $array = 1; - are you unaware of it or is there some other benefit to what you are suggesting?

What might interest people would be a syntactic sugar/shorthand for array_pop but I have no idea what that might look like.

On Fri, 12 Sept 2025 at 11:21, Dušan Kreheľ <dusankrehel@gmail.com> wrote:

A proposal to add syntactic sugar for array_push() in PHP.

Syntax:
// now
array_push($array, 1);

// new syntax

$array[+]=1;

Dušan Kreheľ

http://about.me/kenguest/

I’d personally rather have sugar for array_unshift and array_shift, than pop and push.

— Rob

[+] array_push
[-] array_pop
[-<] array_shift

array_unshift() is a special case of array_merge(), so it would then need syntactic sugar for merging arrays, or rather a reserved merge.

pi 12. 9. 2025 o 16:23 Rob Landers rob@bottled.codes napísal(a):

On Fri, Sep 12, 2025, at 13:06, Dušan Kreheľ wrote:

Hm, I didn’t know about $array = 1;.

It would make sense if it were: $a[+] for push() and $a[-] for pop().

pi 12. 9. 2025 o 12:35 Ken Guest <kguest@php.net> napísal(a):

But we already have $array = 1; - are you unaware of it or is there some other benefit to what you are suggesting?

What might interest people would be a syntactic sugar/shorthand for array_pop but I have no idea what that might look like.

On Fri, 12 Sept 2025 at 11:21, Dušan Kreheľ <dusankrehel@gmail.com> wrote:

A proposal to add syntactic sugar for array_push() in PHP.

Syntax:
// now
array_push($array, 1);

// new syntax

$array[+]=1;

Dušan Kreheľ

http://about.me/kenguest/

I’d personally rather have sugar for array_unshift and array_shift, than pop and push.

— Rob

On Friday 12 September 2025 12:19:19 (+02:00), Dušan Kreheľ wrote:

A proposal to add syntactic sugar for array_push() in PHP.

Syntax:
// now
array_push($array, 1);

// new syntax
$array[+]=1;

    // old
   $array = 1;

Dušan Kreheľ

-- hakre

On 12 September 2025 20:02:04 BST, "Dušan Kreheľ" <dusankrehel@gmail.com> wrote:

[+] array_push
[-] array_pop
[-<] array_shift

array_unshift() is a special case of array_merge(), so it would then need
syntactic sugar for merging arrays, or rather a reserved merge.

array_unshift is no more a special case of array_merge than array_push, particularly in the single-element case we're talking about here - it's really just the same operation at the other end of the array.

Since we have an extremely well-established array_push shorthand, so don't need a new one, we could perhaps have "<" for "at start", and "-" for "pop":

$foo = $toPush;
$popped = $foo[-];
$foo[<] = $toUnshift;
$shifted = $foo[-<];

I'm not totally convinced it's necessary, but I don't completely hate it.

P.S. Quick reminder to all contributors on this thread that the policy here is to reply *below* the relevant quoted text, editing as necessary; do not reply *above* the entire quoted thread.

Rowan Tommins
[IMSoP]

On Sat, Sep 13, 2025 at 1:24 PM Rowan Tommins [IMSoP] <imsop.php@rwec.co.uk> wrote:

On 12 September 2025 20:02:04 BST, “Dušan Kreheľ” <dusankrehel@gmail.com> wrote:

[+] array_push
[-] array_pop
[-<] array_shift

array_unshift() is a special case of array_merge(), so it would then need
syntactic sugar for merging arrays, or rather a reserved merge.

array_unshift is no more a special case of array_merge than array_push, particularly in the single-element case we’re talking about here - it’s really just the same operation at the other end of the array.

Since we have an extremely well-established array_push shorthand, so don’t need a new one, we could perhaps have “<” for “at start”, and “-” for “pop”:

$foo = $toPush;
$popped = $foo[-];
$foo[<] = $toUnshift;
$shifted = $foo[-<];

I’m not totally convinced it’s necessary, but I don’t completely hate it.

I think $item = $list[-] makes sense instead of writing $item = array_pop($list).

I don’t think we should add shorthand operations for array_unshift() and array_shift(), as those are slower operations on array, O(n), compared to array_push() and array_pop() that are O(1).
I mean, we should not make it easier to use, a function is good enough.


Alex

On 2025-09-13 22:21, Rowan Tommins [IMSoP] wrote:

On 12 September 2025 20:02:04 BST, "Dušan Kreheľ" <dusankrehel@gmail.com> wrote:

Since we have an extremely well-established array_push shorthand, so don't need a new one, we could perhaps have "<" for "at start", and "-" for "pop":

$foo[<] = $toUnshift;
$shifted = $foo[-<];

Which looks like it would

$foo[0] $foo[1] $foo[2] <-- put $toUnShift here

rather than

put $toUnshift here --> $foo[0] $foo[1] $foo[2]