php 一般问题
嘿,我正在查看 WP 的一些代码,我注意到在某些情况下,在双引号之间,他们在变量周围放置了大括号。这是一个例子:
$templates[] = "header-{$name}.php";
我尝试在网上查找,但发现很难搜索到这个。有人能解释一下这个/好处的用途吗?
非常感谢。
Hey i was looking through some of WP's code and I noticed in certain cases between double quotes, they put curly brackets around the variable. Here is an example:
$templates[] = "header-{$name}.php";
I tried looking online, but found it difficult to search for this. Would anyone be able to explain the use of this / benefits?
Much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://www.php .net/manual/en/language.types.string.php#language.types.string.syntax.double
向下滚动到变量解析部分以获取详细说明。
基本上,在双引号字符串内,PHP 用变量的内容替换变量。
将变量括在大括号中允许您访问关联数组、对象成员、函数等 (
"{$var['key']} {$foo->bar} {${$foo->baz ()}}"
),并使您的代码更具可读性(恕我直言)http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Scroll way down to the part on variable parsing for a detailed explanation.
Basically, inside a double-quoted string, PHP with replace variables with their contents.
Wrapping the variable in curly braces allows you to access associative arrays, object members, functions, etc (
"{$var['key']} {$foo->bar} {${$foo->baz()}}"
), and makes your code a little more readable (imho)它主要允许您指定数组之类的东西。一个例子是:
它有它的位置,但不需要与上面的示例一起使用。有些人更喜欢它(以帮助他们从字符串中区分变量),而有些人则更喜欢仅使用单引号并连接。
It mainly allows you to specify things like arrays. An example would be:
It has its place, but doesn't need to be used with the example above. Some people prefer it (to help them tell the variables from the string), and some prefer to just use single quotes with concatenation.