将 PHP 函数输出连接到类似变量的字符串


Concatenate PHP function output to a string like variables

对于变量$x,我们可以通过多种方式将其连接到字符串,其中一种如下所示:

$x = "Hello";
echo "I say {$x} to all of you.";
// The output should be: I say Hello to all of you.

但是,如果我尝试用函数做这样的事情,它将失败:

$x = "Hello";
echo "I say {strtolower($x)} to all of you.";
// The output should be: I say {strtolower(Hello)} to all of you.

如果有同义词的方式就像用于变量一样,我将不胜感激。换句话说,我不想拆分主字符串,也不想使用 sprinf .

您可以使用.运算符连接:

echo "I say  " . strtolower($x) . " to all of you.";

或者只是:

echo "I say  ", strtolower($x), " to all of you.";