PHP 变量:引用或副本

发布于 2024-12-09 00:31:35 字数 1523 浏览 0 评论 0原文

我对 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。升级服务器不切实际。

期望的结果:

  1. 如果我忽略了显而易见的事情,请解释一下
  2. 这是一个错误吗?有什么解决办法吗?

谢谢!

-吉姆

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:

  1. Explain the obvious if I've overlooked it
  2. Is this a bug? Any workarounds?

Thanks!

-Jim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧情别恋 2024-12-16 00:31:35

您的代码工作正常,没有错误,并且独立于 PHP 4 或 5。如果向您简单解释一下,也许会有帮助。

让我们看一下这个在您看来不起作用的示例,只是看看实际发生的情况:

// step 1
$foo = array(); # 1.
$foostack = array( &$foo ); # 2.
  • 1.:您将变量 $foo 初始化为一个空数组。
  • 2.:将变量$foostack初始化为一个数组,该数组的第一个元素是变量$foo的别名。这与编写完全相同: $foostack[] =& $foo;

进入下一步:

// step 2
$last = array_pop($foostack); # 3.
array_push($last, 'hello'); # 4.
array_push($foostack, &$last); # 5.
  • 3.:将数组 $foostack 的最后一个元素的 赋给 $last 然后从数组 $foostack 中删除最后一个元素。 注意: array_pop 返回一个值,而不是引用。
  • 4.: 将 'hello' 作为新元素添加到 $last 中的空数组中。
  • 5.: 将 &$last 作为新元素添加到 $foostack 中;

那么我们现在有哪些变量呢?

  • 首先, $foo 只包含一个空数组。 $foostack 的最后一个元素曾经引用过它 (2.),但您在 (3.) 之后直接删除了它。由于$foo并且它的值不再改变,它只是一个空数组array()
  • 然后是$last,它在3.中得到了一个空数组。那只是一个空数组,它是一个值而不是引用。在 (4.) 中,将字符串 'hello' 添加为其中的第一个元素。 $last 是一个包含一个字符串元素的数组。
  • 然后是$foostack。它是一个数组,在 (2.) 中获取对 $foo 的引用,然后在 (3.) 中删除该引用。最后,将 $last 的别名添加到其中。

这正是其余代码的输出:

echo count($foostack[0]) .';'. count($foo);

$foostack[0]$last 的别名 - 带有字符串 'hello' 作为唯一元素,而 $foo 只是 $foo,即空数组 array()

如果您使用 PHP 4 或 5 执行该命令,则没有什么区别。


当您写出“错误”时,我认为您只是无法实现您想要的目标。您可能正在寻找一个能够在删除数组最后一个元素之前返回对它的引用的函数。让我们调用该函数 array_pop_ref()

// step 1
$foo = array();
$foostack = array( &$foo );

// step 2
$last =& array_pop_ref($foostack);
array_push($last, 'hello');
array_push($foostack, &$last);

// results
echo count($foostack[0]) .';' .count($foo); # 1;1

array_pop_ref 函数:

function &array_pop_ref(&$array)
{
    $result = NULL;
    if (!is_array($array)) return $result;
    $keys = array_keys($array);
    $end = end($keys);
    if (false === $end) return $result;
    $result =& $array[$end];
    array_pop($array);
    return $result;
}

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:

// step 1
$foo = array(); # 1.
$foostack = array( &$foo ); # 2.
  • 1.: You initialize the variable $foo to an empty array.
  • 2.: You initialize the variable $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:

// step 2
$last = array_pop($foostack); # 3.
array_push($last, 'hello'); # 4.
array_push($foostack, &$last); # 5.
  • 3.: You assign the last element's value of the array $foostack to $last and you remove the last element from the array $foostack. Note: array_pop returns a value, not a reference.
  • 4.: You add 'hello' as a new element to an empty array in $last.
  • 5.: You add &$last as a new element to $foostack;

So which variables do we have now?

  • First of all $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 array array().
  • Then there is $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.
  • Then there is $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:

echo count($foostack[0]) .';'. count($foo);

$foostack[0] is the alias to $last - the array with the string 'hello' as only element, while $foo is just $foo, the empty array array().

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():

// step 1
$foo = array();
$foostack = array( &$foo );

// step 2
$last =& array_pop_ref($foostack);
array_push($last, 'hello');
array_push($foostack, &$last);

// results
echo count($foostack[0]) .';' .count($foo); # 1;1

The array_pop_ref function:

function &array_pop_ref(&$array)
{
    $result = NULL;
    if (!is_array($array)) return $result;
    $keys = array_keys($array);
    $end = end($keys);
    if (false === $end) return $result;
    $result =& $array[$end];
    array_pop($array);
    return $result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文