php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37141 Variables initialized in function call cannot be passed by reference
Submitted: 2006-04-20 04:50 UTC Modified: 2006-04-20 08:48 UTC
From: jazfresh at hotmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.1.2 OS: Linux Fedora Core 5
Private report: No CVE-ID: None
 [2006-04-20 04:50 UTC] jazfresh at hotmail dot com
Description:
------------
If a variable is initialized inside a function call (e.g. foo($bar, $baz=3) ) and that parameter is a reference, then the variable cannot be altered by the function. It works ok if the variable is initialized outside the function call.

This bug exists in PHP 5.1.2. It does not exist in 5.0.3. I checked the changelogs and release notes for all versions inbetween, but I did not find anything that indicated that the language changed in this way.

Reproduce code:
---------------
function foo(&$bar) {
    echo "Parameter is $bar\n";
    $bar = "baz";
}

foo($woz = "abc");
echo "Woz is $woz\n";

$bliz = "abc";
foo($bliz);
echo "Bliz is $bliz\n";


Expected result:
----------------
Parameter is abc
Woz is baz
Parameter is abc
Bliz is baz


Actual result:
--------------
Parameter is abc
Woz is abc
Parameter is abc
Bliz is baz


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-20 08:25 UTC] tony2001@php.net
Add error_reporting(E_ALL|E_STRICT) to the beginning of your code and you'll see the explanation.
 [2006-04-20 08:43 UTC] jazfresh at hotmail dot com
I did this, and got the message:

PHP Strict Standards:  Only variables should be passed by reference in /path/to/example.php on line 10

However this does not explain the problem, because it is a variable that is being passed, not a constant. In my reproduce code, the function is being called with the variable "$woz". The only difference is that the variable is initialized in the function call, as opposed to outside it.

I would hazard a guess that this is going to break a lot of people's code, because they do things like:

exec('/bin/ls -l', $output = array(), $result);

which will always return an empty array.
 [2006-04-20 08:48 UTC] tony2001@php.net
($var = "value") is an expression, not a variable.
And expressions cannot be referenced.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 23 23:00:03 2025 UTC