变量变量中的美元符号是否被视为取消引用运算符?
我向某人展示了如何在 PHP 中创建可变变量(我只建议永远不要使用它们,这是可怕的做法,如果您在实际生产代码中使用可变变量,那么您就是一个坏人),他们问美元是否在这种情况下,符号充当解引用运算符。
它实际上并没有创建对其他变量的引用,因此我并不真正将其视为 deref 操作。 变量变量的文档甚至根本没有提到引用。
谁是对的?我不认为变量变量正在创建引用,因此美元符号不是取消引用运算符。
以下是一些示例代码,可满足您的观看乐趣(或给您的内容带来痛苦):
<?php
$a = 'c';
$b = 'a';
$c = 'hello';
echo($$$b); //hello
I was showing someone how you can create variable variable variables in PHP (I'd only recommend using them NEVER, it's horrible practice and you are a bad person if you use variable variable variables in actual production code), and they asked if the dollar sign acted as a dereference operator in this case.
It doesn't actually create a reference to the other variables, so I don't really see it as being the deref op. The documentation for variable variables doesn't even mention references at all.
Who's right? I don't think variable variables are creating references and therefore the dollar sign isn't the dereference operator.
Here's some sample code for your viewing pleasure (or pain given the contents):
<?php
$a = 'c';
$b = 'a';
$c = 'hello';
echo($$b); //hello
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。PHP 没有解引用运算符。
变量不应被视为解引用,而是通过字符串访问符号树。例如:
您可以使用变量而不是字符串文字来动态执行此操作:
PHP 语法允许您删除大括号,但仍然需要通过字符串访问符号表。不涉及引用/解除引用。
No. PHP does not possess a dereference operator.
Variable variables shouldn't be thought of as dereferencing but, rather, accessing the symbol tree via a string. For example:
You can perform this dynamically by using a variable instead of a string literal:
PHP syntax allows you to remove the braces but it's still a matter of accessing the symbol table via a string. No referencing/dereferencing involved.
不,它不是取消引用任何东西......如果有的话,它是引用存储变量名称的引用来引用存储变量名称的存储值......有点双重引用或引用的引用.....de-ref 意味着一个变量是另一个变量的子集的一部分。
No, it is not DE-referencing anything....if anything at all, it is referencing the reference of a stored variable name to reference the stored variable name's stored value....kind of a double reference or reference of the reference.....de-ref would mean that one variable was part of the subset of another.