#45780 [Bgs]: why is it only expected for the user data parameter?

[email protected] ("Ultrasick at gmx dot de")
Newsgroups php.bugs
Message-ID <[email protected]>
 ID:               45780
 User updated by:  Ultrasick at gmx dot de
-Summary:          array_walk ignores to pass reference if & is in
                   function definition at userdata
 Reported By:      Ultrasick at gmx dot de
 Status:           Bogus
 Bug Type:         Scripting Engine problem
 Operating System: probably any
 PHP Version:      5.2.6
 New Comment:

So do I also need to add a "&" before the "$my_array" to make changes
in array and not in a copy of the array? Like this:

array_walk(&$my_array, 'my_function', &$my_variable);


Previous Comments:
------------------------------------------------------------------------

[2008-08-11 18:23:34] [email protected]

 

------------------------------------------------------------------------

[2008-08-11 18:22:29] [email protected]

This is expected.

> array_walk($my_array, 'my_function', $my_variable);

The variable is passed by reference to my_function(), you can verify
that by adding an echo in the function.

But as you did not passed $my_variable by reference, array_walk() uses
its own copy of $my_variable, and this is that copy which is passed to
my_function().

So you need to pass it explicitly by reference to array_walk().

------------------------------------------------------------------------

[2008-08-11 03:45:45] Ultrasick at gmx dot de

Description:
------------
The function "array_walk" ignores to pass the reference and passes the
content instead for the parameter "userdata" if the "&" isn't written
inside the "array_walk()" but inside the function definition ("function
somename(&$value, &$key, &$userdata)"). This doesn't work for the
"userdata"-parameter but it does work for the "value" and the
"key"-parameter.

Have a look at the code at the bottom and the expected and the actual
result. Now compare what would happen if you would call "my_function"
manually by using

my_function($any_value, $any_key, $my_variable);
echo $my_variable;

and then using

my_function($any_value, $any_key, &$my_variable);
echo $my_variable;

In both cases $my_variable would be increased, of course.

So in my oppinion the userdata-problem it's not just inconsistency.
It's a bug because a part of the function definition is beeing ignored.

Reproduce code:
---------------
<?
function my_function(&$value, &$key, &$userdata){
	$userdata++;
}

$my_array = array('one', 'two', 'three');
$my_variable = 1;

array_walk($my_array, 'my_function', $my_variable);
echo $my_variable;

echo '<p><hr></p>';

array_walk($my_array, 'my_function', &$my_variable);
echo $my_variable;
?>

Expected result:
----------------
expected output:

4
-----------
4

Actual result:
--------------
actual output:

1
-----------
4


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=45780&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.