|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-01-28 14:01 UTC] mail dot spam at gmx dot net
Description:
------------
if you use a statement as actual parameter for a function (parameter is called by reference) the statement get's executed after calling the function thus overwriting the function's work
Reproduce code:
---------------
function test_array(&$test) {
$test[] = 2;
}
echo '<pre>';
test_array($test_1);
test_array($test_2 = array(1));
$test_3 = array(1);
test_array($test_3);
print_r($test_1);
print_r($test_2);
print_r($test_3);
echo 'PHP-Version: '.phpversion();
echo '</pre>';
Expected result:
----------------
Array
(
[0] => 2
)
Array
(
[0] => 1
[1] => 2
)
Array
(
[0] => 1
[1] => 2
)
Actual result:
--------------
output for version 4.3.11
Array
(
[0] => 2
)
Array
(
[0] => 1
[1] => 2
)
Array
(
[0] => 1
[1] => 2
)
output for version 5.1.2
Array
(
[0] => 2
)
Array
(
[0] => 1
)
Array
(
[0] => 1
[1] => 2
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 04:00:01 2025 UTC |
Thanks for your replay, I reread the manual and found the line you meant But I still think it's a bug: This code: --------- function test_func(&$test_param) { echo 'inside before:'; print_r($test_param); $test_param[] = 2; echo 'inside after:'; print_r($test_param); } echo '<pre>'; echo 'outside before function call:'; print_r($test_array); test_func($test_array = array(1)); echo 'outside after function call:'; print_r($test_array); echo '</pre>'; --------- displays the following: --------- outside before function call: Notice: Undefined variable: test_array in <filename goes here> on line 13 inside before:Array ( [0] => 1 ) inside after:Array ( [0] => 1 [1] => 2 ) outside after function call:Array ( [0] => 1 ) --------- As you can see the expression I try to pass as argument gets evaluated twice. One time before calling the function itself (which is the behaviour like in all earlier version) and a second time after returning from the function call If the expression got evaluated only one time (after returning from function call) I'ld say its ok, you changed things a little bit. But as you seem to execute the code two times I can't imagine why you added this second execution?