Re: [RFC proposal] Syntactic sugar for array push()

From: Date: Sat, 13 Sep 2025 10:55:43 +0000
Subject: Re: [RFC proposal] Syntactic sugar for array push()
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-128698@lists.php.net to get a copy of this message
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

« previous php.internals (#128698) next »