{$var} 和 $var 有什么区别?

发布于 2024-11-09 13:49:10 字数 184 浏览 6 评论 0原文

我想知道何时以及为什么应该使用 {$var}

echo "This is a test using {$var}";

以及何时(以及为什么)应该使用简单形式 $var

echo "This is a test using $var";

I would like to know when and why should I use {$var}

echo "This is a test using {$var}";

and when (and why) should I use the simple form $var

echo "This is a test using $var";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

极度宠爱 2024-11-16 13:49:10

当 a) 不访问对象或数组来获取值,并且 b) 变量名称后面没有可能被解释为变量名称一部分的字符时,您可以使用后者。

You would use the latter when a) not accessing an object or array for the value, and b) no characters follow the variable name that could possibly be interpreted as part of it.

凉宸 2024-11-16 13:49:10

http://php.net/manual/en/language.variables.variable.php

为了将可变变量与数组一起使用,您必须解析一个
歧义问题。也就是说,如果你
写 $$a[1] 然后解析器需要
知道您是否打算使用 $a[1] 作为
变量,或者如果您想要 $$a 作为
变量,然后是 [1] 索引
那个变量。语法为
解决这个歧义的是:${$a[1]}
对于第一种情况和 ${$a}[1] 对于
第二个。

http://php.net/manual/en/language.variables.variable.php

In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you
write $$a[1] then the parser needs to
know if you meant to use $a[1] as a
variable, or if you wanted $$a as the
variable and then the [1] index from
that variable. The syntax for
resolving this ambiguity is: ${$a[1]}
for the first case and ${$a}[1] for
the second.

一绘本一梦想 2024-11-16 13:49:10

在某些特殊情况下,括号允许您消除 PHP 解析器的歧义。
就您而言,它们是等效的。

但请考虑这一点:

$foobar = 'hello';
$foo = 'foo';
echo "${$foo . 'bar'}"; // hello

如果没有括号,您将无法获得预期的结果:

echo "$foo . 'bar'"; // $foo . 'bar'

为了清楚起见,我强烈建议不要使用这种语法。

The brackets allow you to remove ambiguity for the PHP parser in some special cases.
In your case, they are equivalent.

But consider this one:

$foobar = 'hello';
$foo = 'foo';
echo "${$foo . 'bar'}"; // hello

Without the brackets, you will not get the expected result:

echo "$foo . 'bar'"; // $foo . 'bar'

For clarity purposes, I would however strongly advise against this syntax.

恋你朝朝暮暮 2024-11-16 13:49:10

如果您编写

echo "This is a test using $vars"

您不会在结果文本中获得 $var 的内容。

如果你写

echo "This is a test using {$var}s";

一切都会好起来的。

PS 它仅适用于“”,但不适用于“”。

If you write

echo "This is a test using $vars"

You do not get content of $var in result text.

If you write

echo "This is a test using {$var}s";

Everything will be OK.

P.S. It works only with "" but not for ''.

白龙吟 2024-11-16 13:49:10

{} 表示法对于在字符串中嵌入多维数组也很有用。

例如

$array[1][2] = "square";

$text = "This $array[1][2] has two dimensions";

将被解析为

$text = "This " . $array[1] . "[2] has two dimensions";

并且您最终会得到文本

This Array[2] has two dimensions

但是如果您这样做,

$text = "This {$array[1][2]} has two dimensions";

您最终会得到预期的结果

This square has two dimensions.

The {} notation is also useful for embedding multi-dimensional arrays in strings.

e.g.

$array[1][2] = "square";

$text = "This $array[1][2] has two dimensions";

will be parsed as

$text = "This " . $array[1] . "[2] has two dimensions";

and you'll end up with the text

This Array[2] has two dimensions

But if you do

$text = "This {$array[1][2]} has two dimensions";

you end up with the expected

This square has two dimensions.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文