Re: Array spread append
| From: | G. P. B. | Date: | Thu, 06 Apr 2023 13:31:04 +0000 |
| Subject: | Re: Array spread append | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119826@lists.php.net to get a copy of this message | ||
On Thu, 6 Apr 2023 at 14:04, mickmackusa <mickmackusa@gmail.com> wrote:
> I think it would be more intuitive to implement an array "appending"
> functionality with "concatenating" syntax. The
.=
> syntax already exists,
> but is forbidden from use on non-strings. If you want to implement this
> preexisting syntax as an array concatenation operator, then it is a blank
> slate -- you can decree whatever behaviors you wish for it without blurring
> what the combined operator does with strings.
>
This would be the idea situation, but this is not correct.
Arrays, still in PHP 8, get automatically cast to strings during
concatenation or echo:
<?php
$a = [1, 2, 3];
$b = [4, 5, 6];
$a .= $b;
var_dump($a);
?>
Results in:
Warning: Array to string conversion
Warning: Array to string conversion
string(10) "ArrayArray"
See: https://3v4l.org/26Jba
I'm expecting that in PHP 9 this is going to throw a TypeError similarly to
how non-string castable objects throw a TypeError in PHP 8.
But because this was "only" a notice in PHP 7, this specific change only
got promoted to an E_WARNING.
Thus, this suggestion is not possible until the major version, but I do
prefer this compared to the weird $a[...] syntax.
Bbet regards,
George P. Banyard