[RFC proposal] Syntactic sugar for array push()

php.internals

Dušan Kreheľ

354 days ago
A proposal to add syntactic sugar for array_push() in PHP. Syntax: // now array_push($array, 1); // new syntax $array[+]=1; Dušan Kreheľ

Ken Guest

354 days ago
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/

Dušan Kreheľ

354 days ago
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):

Rob Landers

354 days ago
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

Dušan Kreheľ

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

Rowan Tommins [IMSoP]

353 days ago
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]

Alexandru Pătrănescu

353 days ago
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

Weedpacket

352 days ago
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]

Dušan Kreheľ

138 days ago
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):

Rowan Tommins [IMSoP]

137 days ago
On 16 April 2026 16:04:45 BST, "Dušan Kreheľ" <dusankrehel@gmail.com> wrote:
>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]

Dušan Kreheľ

354 days ago
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):

hakre

353 days ago
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