简体   繁体   English

使用Curly Braces构造输出字符串-PHP

[英]Outputting a string using the Curly Braces construct - PHP

When i do either of the two sets of code below i get an parse error and the resulting output doesn't show. 当我执行下面的两组代码中的任何一个时,我都会收到解析错误,并且结果输出不会显示。

Surely there is a way of assigning a value to a variable, creating another variable to output the string, and then echo the variable (using the curly braces{} syntax)? 当然有一种方法可以为变量分配值,创建另一个变量以输出字符串,然后回显该变量(使用花括号{}语法)?

First Example 第一个例子

$name = "Jim ";
$show = echo "{$name}is my name";
echo $show;

Second Example 第二个例子

$name = "Jim ";
$show = echo "{$name}is my name";

Neither of the above work? 以上都不是吗?

The problem is your setting a variable equal to echo: 问题是您将变量设置为等于echo:

$show = echo "{$name}is my name"; $ show = echo “ {$ name}是我的名字”;

This should work: 这应该工作:

$name = "Jim ";
$show = "{$name}is my name";
echo $show;

Check here for a working example: 在这里查看工作示例:

https://ideone.com/ZWDArz https://ideone.com/ZWDArz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM