Req #70087 [Com]: Better string interpolation
| From: | chrisdmiddleton at gmail dot com | Date: | Fri, 17 Jul 2015 16:51:48 +0000 |
| Subject: | Req #70087 [Com]: Better string interpolation | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-194518@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70087&edit=1
ID: 70087
Comment by: chrisdmiddleton at gmail dot com
Reported by: mario dot dweller at seznam dot cz
Summary: Better string interpolation
Status: Open
Type: Feature/Change Request
Package: Strings related
PHP Version: 7.0.0beta1
Block user comment: N
Private report: N
New Comment:
I would also really like this feature, thanks Mario for bringing it up. In Mario's proposal, we
would have to have a BC break, but here's a way that we could do it without a BC break. The
following currently gives a parse error:
echo "{$(time())}";
(Parse error: syntax error, unexpected '(', expecting variable ...)
So it could be implemented that arbitrary expressions would be allowed inside parentheses like that.
On a side note, I've found some very odd behavior in PHP 5.6.10, where I can use a function
call inside variable interpolation, at least in a somewhat limited way. Is this documented
behavior??
<?php
function foo () {
echo "really";
}
echo "${foo()}"; // prints "really"
Previous Comments:
------------------------------------------------------------------------
[2015-07-16 11:52:57] mario dot dweller at seznam dot cz
Description:
------------
There is a support for variable interpolation in strings like
$string = "$a/$b $c";
or
$string = "{$a}/{$b} {$c}";
But it is not usable for functions.
$string = "Length: {strlen($a)}";
I guess that's why many programmers prefer using sprintf instead because it is more readable
than concatenation using dots. I personally use sprintf almost everywhere because the code looks
more consistent then.
Since there are many cool new features in PHP7, why not to add this basic one?
I guess it would need a special syntax because of BC, or not?
But there are currently two ways to achieve the same thing:
$string = "Hello {$a}";
$string = "Hello ${a}";
The second one looks unnaturally, most prefer the first way. For example it could be forbidden in
favor of this:
$string = "Length is ${strlen(a)}";
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70087&edit=1