Professional Web Applications Themes

#45780 [NEW]: array_walk ignores to pass reference if & is in function definition at userdata - PHP Bugs

From: Ultrasick at gmx dot de Operating system: probably any PHP version: 5.2.6 PHP Bug Type: Scripting Engine problem Bug description: array_walk ignores to pass reference if & is in function definition at userdata 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 ...

  1. #1

    Default #45780 [NEW]: array_walk ignores to pass reference if & is in function definition at userdata

    From: Ultrasick at gmx dot de
    Operating system: probably any
    PHP version: 5.2.6
    PHP Bug Type: Scripting Engine problem
    Bug description: array_walk ignores to pass reference if & is in function definition at userdata

    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 bug report at http://bugs.php.net/?id=45780&edit=1
    --
    Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=45780&r=trysnapshot52
    Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=45780&r=trysnapshot53
    Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=45780&r=trysnapshot60
    Fixed in CVS: http://bugs.php.net/fix.php?id=45780&r=fixedcvs
    Fixed in release: http://bugs.php.net/fix.php?id=45780&r=alreadyfixed
    Need backtrace: http://bugs.php.net/fix.php?id=45780&r=needtrace
    Need Reproduce Script: http://bugs.php.net/fix.php?id=45780&r=needscript
    Try newer version: http://bugs.php.net/fix.php?id=45780&r=oldversion
    Not developer issue: http://bugs.php.net/fix.php?id=45780&r=support
    Expected behavior: http://bugs.php.net/fix.php?id=45780&r=notwrong
    Not enough info: http://bugs.php.net/fix.php?id=45780&r=notenoughinfo
    Submitted twice: http://bugs.php.net/fix.php?id=45780&r=submittedtwice
    register_globals: http://bugs.php.net/fix.php?id=45780&r=globals
    PHP 4 support discontinued: http://bugs.php.net/fix.php?id=45780&r=php4
    Daylight Savings: http://bugs.php.net/fix.php?id=45780&r=dst
    IIS Stability: http://bugs.php.net/fix.php?id=45780&r=isapi
    Install GNU Sed: http://bugs.php.net/fix.php?id=45780&r=gnused
    Floating point limitations: http://bugs.php.net/fix.php?id=45780&r=float
    No Zend Extensions: http://bugs.php.net/fix.php?id=45780&r=nozend
    MySQL Configuration Error: http://bugs.php.net/fix.php?id=45780&r=mysqlcfg

    Ultrasick Guest

  2. #2

    Default #45780 [Opn]: array_walk ignores to pass reference if & is in function definition at userdata

    ID: 45780
    Updated by: net
    Reported By: Ultrasick at gmx dot de
    Status: Open
    Bug Type: Scripting Engine problem
    Operating System: probably any
    PHP Version: 5.2.6
    New Comment:

    This is expected.
     

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


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

    [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

    lbarnaud@php.net Guest

  3. #3

    Default #45780 [Opn->Bgs]: array_walk ignores to pass reference if & is in function definition at userdata

    ID: 45780
    Updated by: net
    Reported By: Ultrasick at gmx dot de
    -Status: Open
    +Status: Bogus
    Bug Type: Scripting Engine problem
    Operating System: probably any
    PHP Version: 5.2.6
    New Comment:




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

    [2008-08-11 18:22:29] net

    This is expected.
     

    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

    lbarnaud@php.net Guest

  4. #4

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

    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] net



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

    [2008-08-11 18:22:29] net

    This is expected.
     

    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

    Ultrasick Guest

  5. #5

    Default #45780 [Bgs]: array_walk ignores to pass reference if & is in function definition at userdata

    ID: 45780
    User updated by: Ultrasick at gmx dot de
    -Summary: why is it only expected for the user data parameter?
    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:

    sorry, didn't want to change the summary


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

    [2008-08-11 22:48:48] Ultrasick at gmx dot de

    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);

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

    [2008-08-11 18:23:34] net



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

    [2008-08-11 18:22:29] net

    This is expected.
     

    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

    Ultrasick Guest

  6. #6

    Default #45780 [Bgs]: array_walk ignores to pass reference if & is in function definition at userdata

    ID: 45780
    Updated by: net
    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:

    array_walk is declared as follows:
    bool array_walk ( array &$array , callback $funcname [, mixed
    $userdata ] )

    So $array is implicitly passed by reference.


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

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

    sorry, didn't want to change the summary

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

    [2008-08-11 22:48:48] Ultrasick at gmx dot de

    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);

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

    [2008-08-11 18:23:34] net



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

    [2008-08-11 18:22:29] net

    This is expected.
     

    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

    lbarnaud@php.net Guest

Similar Threads

  1. pass by reference or value?
    By findapollo in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: February 19th, 10:00 AM
  2. function works, array_walk fails
    By Phil Powell in forum PHP Development
    Replies: 0
    Last Post: November 6th, 10:01 PM
  3. Which is better, pass, or not pass by reference?
    By Randell D. in forum PHP Development
    Replies: 6
    Last Post: October 21st, 09:24 PM
  4. Pass by Reference
    By hairybobby webforumsuser@macromedia.com in forum Macromedia Director 3D
    Replies: 3
    Last Post: October 6th, 12:46 PM
  5. Replies: 0
    Last Post: September 7th, 06:53 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139