The example you gave:
function foo() {return "hello";}
echo "${foo()}";
This example won't work because using functions in PHP's {$}
format only allows you to access the result as a variable name and then show the contents of that variable.
In your example, the echo
statement will be trying to echo the contents of a variable named $hello
.
To see why it isn't working, we need to modify the example:
function foo() {return "hello";}
$hello="world";
echo "${foo()}";
This program will print world
. It seems that this is what you've found in the second example you gave.
If you're hoping to get a program that will print hello
in your example, you won't be able to do it using this technique. The good news, however, is that there are plenty of much easier ways to do it. Just echo the function return value.