# [RFC proposal] Syntactic sugar for array push()

- Thread: https://externals.io/message/128679
- Original archive: https://news-web.php.net/php.internals/128679
- Started by dusankrehel@gmail.com on 2025-09-12T10:19:19+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #128679 — dusankrehel@gmail.com — 2025-09-12T10:19:19+00:00

URL: https://externals.io/message/128679#128679

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

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

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

Dušan Kreheľ

---

## #128680 — Ken Guest — 2025-09-12T10:34:54+00:00

URL: https://externals.io/message/128679#128680 · In reply to: #128679

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.



> 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/

---

## #128681 — dusankrehel@gmail.com — 2025-09-12T11:06:04+00:00

URL: https://externals.io/message/128679#128681 · In reply to: #128680

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.
>
> 
>
> > […]
>
> --
> http://about.me/kenguest/

---

## #128682 — dusankrehel@gmail.com — 2025-09-12T14:20:54+00:00

URL: https://externals.io/message/128679#128682 · In reply to: #128679

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> 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ľ

---

## #128683 — Rob Landers — 2025-09-12T14:23:12+00:00

URL: https://externals.io/message/128679#128683 · In reply to: #128681

> 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):
> > […]

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

— Rob

---

## #128686 — dusankrehel@gmail.com — 2025-09-12T19:02:04+00:00

URL: https://externals.io/message/128679#128686 · In reply to: #128683

[+] 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):

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

---

## #128689 — Hans Krentel — 2025-09-12T21:22:55+00:00

URL: https://externals.io/message/128679#128689 · In reply to: #128679

> 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

---

## #128697 — Rowan Tommins [IMSoP] — 2025-09-13T10:21:35+00:00

URL: https://externals.io/message/128679#128697 · In reply to: #128686

>[+] 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]

---

## #128698 — drealecs@gmail.com — 2025-09-13T10:55:43+00:00

URL: https://externals.io/message/128679#128698 · In reply to: #128697

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_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

---

## #128700 — Morgan — 2025-09-13T23:42:26+00:00

URL: https://externals.io/message/128679#128700 · In reply to: #128697

> 
> 
> 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]

---

## #130661 — dusankrehel@gmail.com — 2026-04-16T15:04:45+00:00

URL: https://externals.io/message/128679#130661 · In reply to: #128700

Can someone create 2 votes?:
- [-] for array_pop
- [+] for array_push

I don't have an account and rights; and let the discussion have a practical
outcome (whether with a positive or negative result)?

D. K.


ne 14. 9. 2025 o 1:45 Morgan <weedpacket@varteg.nz> napísal(a):

> 
> > […]
> wrote:
> > […]
> don't need a new one, we could perhaps have "<" for "at start", and "-" for
> "pop":
> > […]
>
> 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]

---

## #130662 — Rowan Tommins [IMSoP] — 2026-04-17T09:11:57+00:00

URL: https://externals.io/message/128679#130662 · In reply to: #130661

>Can someone create 2 votes?:
>- [-] for array_pop
>- [+] for array_push
>
>I don't have an account and rights; and let the discussion have a practical
>outcome (whether with a positive or negative result)?


Hi Dušan,

Anyone is free to propose a change or feature, but before it goes to a vote, it has to be presented in a standard way, called an RFC. Writing that proposal is only a small amount of work, and we can help with parts you done understand, but it's unlikely that somebody will do it all for you.

If you would be interested in following up this proposal, you can see the instructions for creating an RFC here: https://wiki.php.net/rfc/howto

Best regards,

Rowan Tommins
[IMSoP]
