Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand PHP functions in strings just like variables

In PHP I can say

$noun = "bird";
$adjective = "warm;
echo <<<EOT
The $noun is very $adjective
EOT;

and it will output

The bird is very warm

Is there a way to do this with functions?

function getNoun() { return "bird"; }
function getAdjective() { return "warm"; }
echo <<<EOT
The getNoun() is very getAdjective()
EOT;

and output

The bird is very warm

like image 526
charmoniumQ Avatar asked Nov 17 '25 06:11

charmoniumQ


1 Answers

You can use variable functions, though they're rather frowned on upon as they're not far different from variable variables...

$a = 'getNoun';
$b = 'getAdjective';
echo <<<EOT
The {$a()} is very {$b()}
EOT;
like image 164
Marc B Avatar answered Nov 19 '25 21:11

Marc B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!