PHP“&"操作员 [英] PHP "&" operator
问题描述
我不是PHP程序员(但知道其他语言),并且我试图了解一个用PHP(5.1.6)完成的网页,以便进行一些更改.
I'm not a PHP programmer (but know other languages), and I'm trying to understand a web page that was done in PHP (5.1.6) in order to do some changes.
页面具有以下代码(简体):
The page has the following code (simplified):
$db_hosts = array();
$sql = 'SELECT h.hostid, h.host FROM hosts h ';
$db_items = DBselect($sql);
while($db_item = DBfetch($db_items)){
$name = $db_item['host'];
$db_host = &$db_hosts[$db_item['hostid']];
}
我试图了解最后一行$db_host = &$db_hosts[$db_item['hostid']];
.
似乎正在创建一个新变量$db_host
,并在其中添加一些内容,但我不理解&$db_hosts
.
It seems to be creating a new variable, $db_host
, and putting something inside it, but I don't understand &$db_hosts
.
我有疑问,因为据我所知,$db_hosts
是一个空数组.
I'm in doubt because as far as I know, $db_hosts
is an empty array.
我找到了 this 和
I found this and this, but I'm not quite sure, because in these links, the operator is "=&", and in the code, the operator is attached to the variable "= &$db_hosts" (it has an space between = and &).
由于我尝试修改它并没有成功,所以我认为最好寻求帮助...
Since I tried to modify it and didn't get success, I thought that it was better to ask for help...
推荐答案
这些都是引用,它们类似于C或C ++中的指针".
Those are references, and they are similar to "pointers" in C or C++.
更多信息,请参见 PHP手册.
在这种情况下,由于$db_hosts
为空,因此构造$db_hosts[$db_item['hostid']]
将创建一个新数组,该数组在$db_item['hostid']
的索引上具有一个空项目,并返回该项目的引用,从而使$db_host
充当别名" "表示当前的$db_hosts[$db_item['hostid']]
.
In this case, since $db_hosts
is empty, the construct $db_hosts[$db_item['hostid']]
will create a new array with an empty item on the index of $db_item['hostid']
and return the item's reference, making $db_host
act as an 'alias' for whatever $db_hosts[$db_item['hostid']]
is currently.
这篇关于PHP“&"操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!