在 PHP 中,${$} 语法有什么作用? [英] In PHP, what does the ${$ } syntax do?

查看:39
本文介绍了在 PHP 中,${$} 语法有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WordPress 插件中遇到了这个问题.

I ran across this inside a WordPress plugin.

$stuff = $wpdb->get_results(" assume valid database query here ");
foreach ($stuff as $cur)
    ${$cur->type}[] = $cur->name;

$stuff 将是一个包含更多数据库行对象的对象.这些数据库行将包含 'id''type''name' 列.'type' 列将包含以下三个字符串之一:'file''url''code'.

$stuff will be an object containing more objects of database rows. These database rows will have columns 'id', 'type', and 'name'. The 'type' column will contain one of these three strings: 'file', 'url', or 'code'.

看起来这个代码片段可能会为名为 $file$url 和/或 $code 的数组创建或添加新元素>.但是,我不熟悉 ${$ } 语法的这种用法;我只在双引号字符串中看到它以避免解析问题.

It looks like this code snippet will potentially create or add new elements to arrays named $file, $url, and/or $code. However, I'm not familiar with this use of the ${$ } syntax; I've only seen it inside double quoted strings to avoid parsing problems.

我对这段代码的分析是否正确?在哪里可以详细了解 ${$} 语法的这种用法?

Am I correct in my analysis of this code? Where can I learn more about this use of the ${$ } syntax?

在双引号字符串中有一个关于 ${ } 语法的问题.我理解这种用法​​,但我特别询问 { } 大括号内的第二个 $ 字符.

There is a question about the ${ } syntax inside a double-quoted string. I understand that use, but I'm specifically asking about a second $ character inside the { } braces.

推荐答案

考虑

$foo = 42;
$a = 'foo';
echo $$a; // Prints 42

这被称为可变变量,因为变量的名称是在运行时确定的.但是 $$a[1] 与 ${$a[1]} 相同还是与 {$$a}[1] 相同?括号避免了这种歧义,就像它们在处理数学中的运算符优先级时所做的一样.

That´s called a variable variable, since the variable´s name is determinated at runtime. But is $$a[1] the same as ${$a[1]} or the same as {$$a}[1]? The brackets avoid that ambiguity, just like they do when dealing with operator precedence in math.

这篇关于在 PHP 中,${$} 语法有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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