Concatenate PHP function output to a string like variables

被刻印的时光 ゝ 提交于 2019-12-13 08:35:43

问题


For variable $x we can concatenate it to a string in many ways, one of them is like the following:

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

However, If I tried to do something like this with a function, it will fail:

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

If there is a synonym way just like used for the variable, I will be appreciated to know it. In other words, I don't want to split the main string and I don't want to use sprinf.


回答1:


You can concatenate with . operator:

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

Or just :

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


来源:https://stackoverflow.com/questions/27494038/concatenate-php-function-output-to-a-string-like-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!