PHP 中奇怪的数组创建?
我正在阅读 mysqli_stmt_bind_result 的 PHP 手册 并看到此代码位于 注释:
while ( $field = $meta->fetch_field() ) {
$parameters[] = &$row[$field->name];
}
鉴于 $params 和 $row 都不存在于该行之前,为什么/该行如何工作?
I was reading the PHP manual for mysqli_stmt_bind_result and saw this code in the comments:
while ( $field = $meta->fetch_field() ) {
$parameters[] = &$row[$field->name];
}
given that neither $params nor $row existed before that line, why/how does that line work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PHP 实际上没有变量声明。这意味着在某些情况下,您可以引用变量而无需事先声明它们。我说某些情况是因为:
但这并不意味着你不能做这样的事情:
这是有效的,因为
$undefinedArray
不是即时找到和创建的。现在,关于你自己的情况。我假设你的意思是这篇文章。我必须承认,这是一个非常有趣的解决方案,我会尽力克制自己不对任何不良做法发表评论,但让我们继续解释它!
这就是神奇发生的地方,这实际上是由于 & 造成的。因为
&$row['unknown_index']
,实际上创建了索引!这意味着上面的语句做了两件事。首先,它创建一个数组,其中每个列名称保存为
$row
中的索引($row[$field->name]
)。然后,它在$params
中保存指向$row
中每个元素的指针。这会执行
$stmt->bind_result()
。而是将$params
中的每个元素作为参数传递给 bind_result。由于它们是通过引用传递的,因此 $row 的每个索引将保存每个选定的字段。剩下的现在应该很容易弄清楚了。
如果您有任何疑问。欢迎询问!
PHP doesn't actually have variable declaration. That means in some cases you can reference variables without actually having them declared beforehand. I say some cases because:
But this doesn't mean you can't do something like so:
This works because
$undefinedArray
is not found and created on the fly.Now, regarding your own case. I'm gonna assume you mean this post. And I have to admit, that's a very interesting solution, I'm gonna try to restrain myself from commenting on any kind of bad practice there, but let's get on to explaining it!
This is where the magic happens and it's actually due to the &. Because
&$row['unknown_index']
, actually creates the index!This means that above statement does 2 things. First it creates an array with each column name saved as an index in
$row
($row[$field->name]
). Then it saves a pointer to each of the elements from$row
in$params
.This does
$stmt->bind_result()
. But passes each of the elements in$params
as parameters to bind_result. And since they're passed by reference, each index of $row will hold each of the selected fields.The rest should be easy to figure out now.
If you got any questions. Feel free to ask!
从第一条评论开始。
那么问题出在哪里呢?
From the first comment.
So what's the problem?
它不会起作用,因为正如你所说,这些变量不存在。
It won't work because as you said, these variables do not exist.
来自 C、C++ 编程学校,我有时很难接受 PHP 的堆栈和堆管理。 PHP 自动完成太多的事情,这给了开发人员太多的自由。无论如何,我相信在 Dexter 的精彩解释之后,他的疑虑已经消除。 com/users/688411/khez">赫兹。我只是想在代码中添加我的美分,以便更好地理解提到的代码中发生的事情 此处。
就是这样。希望这段代码有意义。
PS:事后想澄清一下我对PHP的一个疑惑。如果我尝试按如下方式填充 $results:
或者像这样:
$results 中的所有键/值对集重复包含相同的集。希望有人能对 PHP 的这种行为有所启发。
php-5.3 mysqli
Being from C,C++ school of programming, i sometimes struggle to come to terms with PHP's stack and heap management. PHP does way too many things automatically, which gives way too much freedom to developers. Anyways, am sure Dexter has got his doubts cleared after that wonderful explanation from Khez. I just want to add my cents in the code to make more sense of whats happening in the code mentioned here.
So that's it. Hopefully this code will make sense.
P.S: As an afterthought, I want to clarify one of my doubts regarding PHP. If I try to populate $results as below:
Or like this:
All sets of key/value pairs in $results contain the same set repeatedly. Hopefully somebody and throw some light on this behavior of PHP.
php-5.3 mysqli