是否可以在PHP字符串中扩展函数调用?

Mis*_*hko 3 php string

我试着在foo()里面调用这样的字符串:

echo "This is a ${foo()} car";

function foo() {
    return "blue";
}
Run Code Online (Sandbox Code Playgroud)

但是,它最终会出现语法错误.

我在这里找到了类似的东西,但不完全是我需要的东西:

echo "This is the value of the var named by the return value of getName(): {${getName()}}";
Run Code Online (Sandbox Code Playgroud)

这有可能吗?

wim*_*vds 8

可能吗?没有.

自PHP 5起,{$}内的函数,方法调用,静态类变量和类常量工作.但是,访问的值将被解释为定义字符串的作用域中变量的名称.使用单个花括号({})将无法访问函数或方法的返回值或类常量或静态类变量的值.

这是http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex上卷曲语法的第一个注释.