PHP 变量:引用或副本
我对 PHP 变量引用的工作原理感到困惑。在下面的示例中,我希望能够以 $bar[0]
或 $barstack[0][0]
形式访问字符串 hello代码>.看来在步骤 1 中通过引用传递数组就足够了。
第二个例子不起作用。 $foostack[0]0]
是字符串 hello,但 $foo[0]
不存在。在某些时候,$foostack 的第一个元素会成为 $foo 的副本,而不是引用。
问题出在步骤 2 的第一行:当我推入引用时,我希望弹出引用。但 array_pop
返回一个副本。
其他人告诉我,如果我必须担心引用和副本,那么 PHP 就不是适合我的语言。这可能是我能得到的最好答案。
FWIW,为了使 var_dump 有用,它需要显示一些区分引用和副本的属性。事实并非如此。也许还有另一个功能?
我的第一个 PHP 项目似乎进展得很糟糕。有人可以帮忙解释一下吗 这段代码有问题吗?
<?php
echo "// This works!\n<br />" ;
// step 1
$bar = array() ;
$barstack = array( &$bar ) ;
// step 2
array_push( $barstack[0], 'hello' ) ;
// results
echo count( $barstack[0] ) .';' .count( $bar ) ;
echo "\n<br />// This doesn't :(\n<br />" ;
// step 1
$foo = array() ;
$foostack = array( &$foo ) ;
// step 2
$last = array_pop( $foostack ) ;
array_push( $last, 'hello' ) ;
array_push( $foostack, &$last ) ;
// results
echo count( $foostack[0] ) .';' .count( $foo ) ;
echo "\n<br />// Version:\n<br />" ;
echo phpversion() ."\n" ;
?>
可以在以下 URL 查看结果:
http://www.gostorageone.com/tqis/hi.php< /p>
版本是 4.3.10。升级服务器不切实际。
期望的结果:
- 如果我忽略了显而易见的事情,请解释一下
- 这是一个错误吗?有什么解决办法吗?
谢谢!
-吉姆
I'm confused about how PHP variable references work. In the examples below, I want to be able to access the string hello either as $bar[0]
or $barstack[0][0]
. It would seem that passing the array by reference in step 1 should be sufficient.
The second example does not work. $foostack[0]0]
is the string hello, but $foo[0]
doesn't exist. At some point, the first element of $foostack becomes a copy of $foo, instead of a reference.
The problem lies in the first line of step 2: When I push a reference on, I expect to pop a reference off. But array_pop
returns a copy instead.
Others have told me that if I have to worry about references and copies, then PHP is not the right language for me. That might be the best answer I'm going to get.
FWIW, in order for var_dump
to be useful, it needs to display some property that distinguishes between a reference and a copy. It does not. Maybe there's another function?
My first PHP project seems to be going badly. Can someone help shed some light on the
problems with this code?
<?php
echo "// This works!\n<br />" ;
// step 1
$bar = array() ;
$barstack = array( &$bar ) ;
// step 2
array_push( $barstack[0], 'hello' ) ;
// results
echo count( $barstack[0] ) .';' .count( $bar ) ;
echo "\n<br />// This doesn't :(\n<br />" ;
// step 1
$foo = array() ;
$foostack = array( &$foo ) ;
// step 2
$last = array_pop( $foostack ) ;
array_push( $last, 'hello' ) ;
array_push( $foostack, &$last ) ;
// results
echo count( $foostack[0] ) .';' .count( $foo ) ;
echo "\n<br />// Version:\n<br />" ;
echo phpversion() ."\n" ;
?>
The results can be viewed at the following URL:
http://www.gostorageone.com/tqis/hi.php
Version is 4.3.10. Upgrading the server is not practical.
Desired outcomes:
- Explain the obvious if I've overlooked it
- Is this a bug? Any workarounds?
Thanks!
-Jim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码工作正常,没有错误,并且独立于 PHP 4 或 5。如果向您简单解释一下,也许会有帮助。
让我们看一下这个在您看来不起作用的示例,只是看看实际发生的情况:
$foo
初始化为一个空数组。$foostack
初始化为一个数组,该数组的第一个元素是变量$foo
的别名。这与编写完全相同:$foostack[] =& $foo;
进入下一步:
$foostack
的最后一个元素的 值 赋给$last
然后从数组$foostack
中删除最后一个元素。 注意:array_pop
返回一个值,而不是引用。'hello'
作为新元素添加到$last
中的空数组中。&$last
作为新元素添加到$foostack
中;那么我们现在有哪些变量呢?
$foo
只包含一个空数组。$foostack
的最后一个元素曾经引用过它 (2.),但您在 (3.) 之后直接删除了它。由于$foo
并且它的值不再改变,它只是一个空数组array()
。$last
,它在3.中得到了一个空数组。那只是一个空数组,它是一个值而不是引用。在 (4.) 中,将字符串'hello'
添加为其中的第一个元素。$last
是一个包含一个字符串元素的数组。$foostack
。它是一个数组,在 (2.) 中获取对$foo
的引用,然后在 (3.) 中删除该引用。最后,将$last
的别名添加到其中。这正是其余代码的输出:
$foostack[0]
是$last
的别名 - 带有字符串'hello' 作为唯一元素,而
$foo
只是$foo
,即空数组array()
。如果您使用 PHP 4 或 5 执行该命令,则没有什么区别。
当您写出“错误”时,我认为您只是无法实现您想要的目标。您可能正在寻找一个能够在删除数组最后一个元素之前返回对它的引用的函数。让我们调用该函数
array_pop_ref()
:array_pop_ref
函数:Your code works fine, there is no bug, and it is independent to PHP 4 or 5. Maybe it helps if this is simply explained to you.
Let's go through the example which does not work in your eyes, just looking what actually happens:
$foo
to an empty array.$foostack
to an array and the first element of the array is an alias of the variable$foo
. This is exactly the same as writing:$foostack[] =& $foo;
On to the next step:
$foostack
to$last
and you remove the last element from the array$foostack
. Note:array_pop
returns a value, not a reference.'hello'
as a new element to an empty array in$last
.&$last
as a new element to$foostack
;So which variables do we have now?
$foo
which just contains an empty array. The last element of$foostack
was once reference to it (2.), but you have removed that directly after (3.). As$foo
and it's value has not been changed any longer, it's just an empty arrayarray()
.$last
, which got an empty array in 3.. That's just an empty array, it's a value not a reference. In (4.) you add the string'hello'
as first element to it.$last
is an array with one string element in there.$foostack
. It's an array that get's a reference to$foo
in (2.), then that reference is removed in (3.). Finally an alias to$last
is added to it.This is exactly what the rest of your code outputs:
$foostack[0]
is the alias to$last
- the array with the string'hello'
as only element, while$foo
is just$foo
, the empty arrayarray()
.It makes no difference if you execute that with PHP 4 or 5.
As you write that's "wrong", I assume you were just not able to achieve what you wanted. You're probably looking for a function that is able to return the reference to the last element of an array before removing it. Let's call that function
array_pop_ref()
:The
array_pop_ref
function: