php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36191 statement as function parameter gets evaluated after calling the function
Submitted: 2006-01-28 14:01 UTC Modified: 2006-01-30 08:52 UTC
From: mail dot spam at gmx dot net Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.1.2 OS: Windows
Private report: No CVE-ID: None
 [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
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-29 16:08 UTC] sniper@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2006-01-30 08:52 UTC] mail dot spam at gmx dot net
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?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 18 19:00:03 2025 UTC