变量变量中的美元符号是否被视为取消引用运算符?

发布于 2024-12-03 15:23:35 字数 424 浏览 0 评论 0原文

我向某人展示了如何在 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 技术交流群。

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

发布评论

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

评论(2

丑丑阿 2024-12-10 15:23:35

变量中的美元符号是否被视为解除引用
运算符?

不。PHP 没有解引用运算符。

变量不应被视为解引用,而是通过字符串访问符号树。例如:

$bar = 1;
echo ${'bar'};

您可以使用变量而不是字符串文字来动态执行此操作:

$bar = 1;
$foo = 'bar';
echo ${$foo};

PHP 语法允许您删除大括号,但仍然需要通过字符串访问符号表。不涉及引用/解除引用。

Is the dollar sign in a variable variable considered the dereference
operator?

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:

$bar = 1;
echo ${'bar'};

You can perform this dynamically by using a variable instead of a string literal:

$bar = 1;
$foo = 'bar';
echo ${$foo};

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.

橙幽之幻 2024-12-10 15:23:35

不,它不是取消引用任何东西......如果有的话,它是引用存储变量名称的引用来引用存储变量名称的存储值......有点双重引用或引用的引用.....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.

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