问题
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
回答1:
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 arrayarray()
. - 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;
}
来源:https://stackoverflow.com/questions/7691510/php-variables-references-or-copies