在PHP中,我可以说
$noun = "bird";
$adjective = "warm;
echo <<<EOT
The $noun is very $adjective
EOT;
并输出
这只鸟很暖和
有没有一种方法可以用函数来实现这一点?
function getNoun() { return "bird"; }
function getAdjective() { return "warm"; }
echo <<<EOT
The getNoun() is very getAdjective()
EOT;
并输出
这只鸟很暖和
您可以使用变量函数,尽管它们与变量变量没有太大区别,因此不受欢迎。。。
$a = 'getNoun';
$b = 'getAdjective';
echo <<<EOT
The {$a()} is very {$b()}
EOT;
您可以在使用它之前将其存储在一个变量中:
$noun = getNoun( );
$adj = getAdjective( );
echo <<<EOT
The {$noun} is very {$adj}
EOT;
echo "The ";
echo getNoun();
echo " is very ";
echo getVerb();
或者甚至(不是100%确定(:
echo "The " . getNoun() . " is very " . getVerb();
这里有一个使用可变函数的解决方案,尽管不完全是:
$noun=function($type){
return $type;
};
$adjective=function($adj){
return $adj;
};
echo "{$noun("Robin")} was very {$adjective("Warm")}";