将 PHP 函数输出连接到类似变量的字符串 [英] Concatenate PHP function output to a string like variables

查看:25
本文介绍了将 PHP 函数输出连接到类似变量的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

You can concatenate with . operator:

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

或者只是:

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

这篇关于将 PHP 函数输出连接到类似变量的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆