php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70087 Better string interpolation
Submitted: 2015-07-16 11:52 UTC Modified: 2021-05-28 11:09 UTC
Votes:5
Avg. Score:3.4 ± 2.0
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: mario dot dweller at seznam dot cz Assigned:
Status: Suspended Package: Strings related
PHP Version: 7.0.0beta1 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mario dot dweller at seznam dot cz
New email:
PHP Version: OS:

 

 [2015-07-16 11:52 UTC] 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)}";`



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-16 15:04 UTC] cmb@php.net
-Package: PHP Language Specification +Package: Strings related
 [2015-07-17 16:51 UTC] chrisdmiddleton at gmail dot com
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"
 [2015-07-17 16:58 UTC] chrisdmiddleton at gmail dot com
Never mind my previous question about function calls in variable interpolation. To answer my own question, I see that it is documented in the manual.
 [2020-07-13 05:19 UTC] vovan-ve at yandex dot ru
A good idea is needed here. Supposed syntax `"{$(expr)}"` is complicated in reading and writing.

It would be great feature. Take a look at JavaScript's template strings:

```js
const s = foo`lorem ${bar(x)} ipsum ${2 + 3} dolor`;
```

Many other progressive languages has unified interpolation syntax for arbitrary expression.

I'm observing your RFC's <https://wiki.php.net/rfc> for more than 10 years. Lot's of good things were declined like it was just conservative. Any good feature will be declined and forgotten when you hit your own internal implementation problems and conservative views.
 [2021-05-28 11:09 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-05-28 11:09 UTC] cmb@php.net
This feature would require to think about edge cases[1] and
generally discussion for which this bug tracker isn't apt.
Anybody who is interested in this may pursue the RFC process[2].
For the time being, I suspend this ticket.

[1] <https://externals.io/message/111519>
[2] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC