php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #21433 Generic expressions interpolation in strings
Submitted: 2003-01-05 10:35 UTC Modified: 2003-01-05 13:48 UTC
From: giannici at neomedia dot it Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.2.2 OS: Any
Private report: No CVE-ID: None
 [2003-01-05 10:35 UTC] giannici at neomedia dot it
I'm relatively new to PHP.
Previously I used to program a lot in Perl.
What I really lack in PHP is Perl's ability to interpolate any expression inside a string.
For example:

Perl: "Next value is @{[ $value + 1 ]}!"
PHP:  "Next value is " . $value + 1 . "!"

In this simple case it's not a great problem, but it becomes more awkward when using the heredoc operator. In this cases I'm often forced to use temporary variables:

$tempvar = $value + 1;
print <<< END
Here it is the next value

$tempvar

Other text...

END;


I propose to extend the Variable Parsing syntax to evaluate ANY expression instead of variables only.
I propose the following syntax:

print <<< END
Here it is the next value

{$= $value + 1 }

Other text...

END;


Using "{=" would be more readable but it would cause too many backward compatibility problems...

What do you think of this?

Thanks.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-05 13:48 UTC] tularis@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

I don't think this will be implemented, since this is partially why heredocs were created.

The only thing I can tell you is, you should use stuff like:
echo "it is: ".++$value;
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 16:01:34 2025 UTC