php 一般问题

发布于 2024-11-06 22:09:38 字数 186 浏览 0 评论 0原文

嘿,我正在查看 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 技术交流群。

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

发布评论

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

评论(2

苍景流年 2024-11-13 22:09:38

http://www.php .net/manual/en/language.types.string.php#language.types.string.syntax.double

向下滚动到变量解析部分以获取详细说明。

基本上,在双引号字符串内,PHP 用变量的内容替换变量。

$var = 'pig';
echo "Hello, $var"; // echos Hello, pig

将变量括在大括号中允许您访问关联数组、对象成员、函数等 ("{$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.

$var = 'pig';
echo "Hello, $var"; // echos Hello, pig

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)

戏舞 2024-11-13 22:09:38

它主要允许您指定数组之类的东西。一个例子是:

$arr = array("mon"=>"Monday","tue"=>"Tuesday");
echo "Today is {$arr["mon"]}";

它有它的位置,但不需要与上面的示例一起使用。有些人更喜欢它(以帮助他们从字符串中区分变量),而有些人则更喜欢仅使用单引号并连接。

It mainly allows you to specify things like arrays. An example would be:

$arr = array("mon"=>"Monday","tue"=>"Tuesday");
echo "Today is {$arr["mon"]}";

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.

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