Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, How to call a function interpolated in string?

Tags:

php

Let's say string is:

$str="abcdefg foo() hijklmopqrst";

How do I let php call foo() and insert the returning string to this string?

like image 242
lovespring Avatar asked Apr 03 '12 22:04

lovespring


1 Answers

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

<?php
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.

like image 177
HonoredMule Avatar answered Sep 29 '22 21:09

HonoredMule