32

PHP supports variable interpolation in double quoted strings, for example,

$s = "foo $bar";

But is it possible to interpolate function call results in the double quoted string?

For example,

$s = "foo {bar()}";

Something like that? It doesn't seem not possible, right?

3
  • 2
    yes it is not possible. You can get your answer simply by trying Commented May 25, 2012 at 17:03
  • Did you mean interpolating or interpreting? Commented May 31, 2012 at 13:09
  • 2
    Interpolating is the correct word here. Commented Jan 8, 2020 at 2:05

4 Answers 4

33

It is absolutely possible using the string-to-function-name calling technique as Overv's answer indicates. In many trivial substitution cases it reads far better than the alternative syntaxes such as

"<input value='<?php echo 1 + 1 + foo() / bar(); ?>' />"

You need a variable, because the parser expects the $ to be there.

This is where the identity transform works well as a syntactic hack. Just declare an identity function, and assign the name to a variable in scope:

function identity($arg){return $arg;}
$interpolate = "identity";

Then you can pass any valid PHP expression as the function argument:

"<input value='{$interpolate(1 + 1 + foo() / bar() )}' />"

The upside is that you can eliminate a lot of trivial local variables and echo statements.

The downside is that the $interpolate variable falls out of scope, so you would have to repeatedly declare it global inside of functions and methods.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, let's hope this goes through: wiki.php.net/rfc/arbitrary_expression_interpolation
@jerseyboy You could even shorten that to $interpolate = fn($arg) => $arg;
4

The double quote feature in PHP does not evaluate PHP code, it simply replaces variables with their values. If you want to actually evaluate PHP code dynamically (very dangerous), you should use eval:

eval( "function foo() { bar() }" );

Or if you just want to create a function:

$foo = create_function( "", "bar()" );
$foo();

Only use this if there really is no other option.

2 Comments

If the function was inside a class, then you can refer it inside string, for e.g.: $s = "foo {$this->bar()}";
@nawfal and the {} are required. Otherwise php will look for bar property.
3

Unless interpolation is absolutely necessary (please comment and let me know why), concatenate the function output with the string.

$s =  "foo " . bar();

3 Comments

Ok but actually this does not answer the question ;-)
Not, but it does give the correct solution to the underlying problem, which is to construct a string consisting of both literals parts and expressions.
This is the only sane answer out of whole thread.
-1

You can't do that. However, as proposed in this answer, you can trick the constraint by having a function as variable, or in my case, an instance of a home-made class :

class StringInterpolatorWrapper
{
    public function i(mixed $message): mixed
    {
        return $message;
    }
}

$f = new StringInterpolatorWrapper();
$complex_string = <<<HTML
<input value="{$f->i(1 + 1 + foo() / bar())}" />
HTML;

Wherever you need it, just import the class and create an instance.

You can add other methods to this class, to fit your needs, create shortcuts for regularly used function inside {$f->i(my_function($my_var))}.

2 Comments

And now there is a question. Why not to make it just $var = 1 + 1 + foo() / bar(); $complex_string = "<input value='$var' />";? Without shortcuts, without importing the class, without creating an instance, without obfuscating your own code.
Sometimes it is just quicker and easier, it is not just black and white man. Use your common sense to find examples where having to create a variable for each values you want to be inserted is a pain in the a$$.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.