Re: [RFC][Under discussion] Arbitrary string interpolation
| From: | Robert Landers | Date: | Sat, 19 Mar 2022 18:35:09 +0000 |
| Subject: | Re: [RFC][Under discussion] Arbitrary string interpolation | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117382@lists.php.net to get a copy of this message | ||
On Fri, Mar 18, 2022 at 11:01 PM Theodore Brown <theodorejb@outlook.com>
wrote:
> On Fri, Mar 18, 2022 at 09:02 Chase Peeler <chasepeeler@gmail.com> wrote:
> > On Fri, Mar 18, 2022 at 12:49 AM Theodore Brown <theodorejb@outlook.com>
> wrote:
> > >
> > > Personally I'm really looking forward to having this functionality.
> > > Just a couple days ago I wanted to call a function in an interpolated
> > > string, and it was really annoying to have to wrap the function in a
> > > closure in order to use it.
> > >
> > > If this RFC is accepted I'd be able to replace code like this:
> > >
> > > $name = "Theodore Brown";
> > > $strlen = fn(string $string): int => strlen($string);
> > > echo "{$name} has a length of {$strlen($name)}.";
> > >
> > > with
> > >
> > > $name = "Theodore Brown";
> > > echo "{$name} has a length of {$:strlen($name)}.";
> >
> >
> > Out of curiosity, why not:
> >
> > $name = "Theodore Brown";
> > echo "{$name} has a length of ".strlen($name).".";
> >
> > or even
> >
> > $name = "Theodore Brown";
> > $len = strlen($name);
> > echo "{$name} has a length of {$len}.";
>
> Concatenation works fine for a simple example like this, but it can
> get a lot messier when there are more than a few embedded variables.
> It's particularly an issue with heredoc strings which are far more
> cumbersome to concatenate.
>
> Yes, it's possible to add extra variables before the string like in
> your second example, but this feels like unnecessary work, especially
> when you already have all the variables you want and just want to
> apply a function to them at several places in a template string.
>
One function that I continuously run into is 'number_format' (and its intl
equivalents). It'd be much easier to read "you have
{$:number_format(count($items))} remaining" instead of "you have
$formatted_count_of_items remaining"
> Theodore
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>