Hi everyone
String interpolations are a great feature. They make your code more readable and can reduce the number of allocations PHP has to make.
Unfortunately, PHP is very restrictive in what kind of expressions string interpolation accepts. The only time you can actually use it is if your expression starts with the $
symbol. You cannot use it for:
- Function calls
- Infix expressions
- Constants or class constants
- Instantiations
- Expressions in parentheses
Or anything else that doesn’t begin with the $
symbol.
Even more unfortunate is that ${foo()}
is equivalent to {$foo()}
which means we cannot simply extend the current syntax to support all expressions. Of course, we could deprecate the ${foo()}
syntax and reintroduce it with a different semantic meaning after some time but this is far from satisfying.
There is also the possibility of introducing new syntax for string interpolation and deprecating the old one after a transition period (e.g. `”Hello (world())”).
Does anyone think this is worth fixing?
Oops, I actually meant to suggest the syntax “Hello \{world()}”
(note: curlies instead of parens) to be closer to the original syntax.
Regards,
Ilija
Hi everyone
String interpolations are a great feature. They make your code more readable and can reduce the number of allocations PHP has to make.
Unfortunately, PHP is very restrictive in what kind of expressions string interpolation accepts. The only time you can actually use it is if your expression starts with the
$
symbol. You cannot use it for:
- Function calls
- Infix expressions
- Constants or class constants
- Instantiations
- Expressions in parentheses
Or anything else that doesn’t begin with the
$
symbol.Even more unfortunate is that
${foo()}
is equivalent to{$foo()}
which means we cannot simply extend the current syntax to support all expressions. Of course, we could deprecate the${foo()}
syntax and reintroduce it with a different semantic meaning after some time but this is far from satisfying.There is also the possibility of introducing new syntax for string interpolation and deprecating the old one after a transition period (e.g. `”Hello (world())”).
Does anyone think this is worth fixing?