On Fri, Mar 18, 2022 at 11:01 PM Theodore Brown <theodor...@outlook.com> wrote:
> On Fri, Mar 18, 2022 at 09:02 Chase Peeler <chasepee...@gmail.com> wrote: > > On Fri, Mar 18, 2022 at 12:49 AM Theodore Brown <theodor...@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 > >