Php – In PHP, How to call function in string


say,string is:

$str="abcdefg foo() hijklmopqrst";

How to let php call foo() and insert the return string to this string?

Best Solution

If you're calling a method of some class, you can use normal variable expansion. For example:

class thingie {

  public function sayHello() {
    return "hello";


$t = new thingie();
echo "thingie says: {$t->sayHello()}";

This will output:

thingie says: hello

Note that the braces around the call are required.