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.
A proposal to add syntactic sugar for
array_push()
in PHP.Syntax:
// now
array_push($array, 1);// new syntax
$array[+]=1;Dušan Kreheľ
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ľ
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ľ
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):
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ľ
I'd personally rather have sugar for array_unshift and array_shift, than
pop and push.— Rob
[+] array_push
[-] array_pop
[-<] array_shiftarray_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_shiftarray_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
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]
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ľ
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