php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #42674 missing error when assigning vars in reference arguments
Submitted: 2007-09-14 17:48 UTC Modified: 2007-11-21 16:34 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: Slig at free dot fr Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5CVS-2007-09-14 (snap) OS: linux-64
Private report: No CVE-ID: None
 [2007-09-14 17:48 UTC] Slig at free dot fr
Description:
------------
I know it has already be discused in #35995.

I do not really agree with that feature (it's accepted by other languages like C, and after assignment it's a variable which is passed to the function), but if this syntax is forbidden then it should make an error, else it is really difficult to fix it in old programs.

Also, because until 5.1.1 it worked like some of us expected, would be nice to add some warning about it in the documentation.

Reproduce code:
---------------
<?php
function myFunc(&$var1) {
    $var1 = 'set in function';
}

$var2 = 'set before function';
myFunc($var2 = 'set in function call');

var_dump($var2);
?>

Expected result:
----------------
Fatal error: Cannot pass parameter 1 by reference in xxxx at line nn

Actual result:
--------------
No error, but the function get in $var1 the assigned value, and after the call $var2 get the assign value too (so not changed by the function)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-18 10:46 UTC] jani@php.net
You do get an error, with E_STRICT enabled:

Strict Standards: Only variables should be passed by reference in /home/jani/t.php on line 7

Reclassified as documentation issue.
 [2007-09-19 08:16 UTC] Slig at free dot fr
Ok, i didn't noticed it, thx  :)

Anyway, why  "MyFunc($var2 = 'set in function call');"  need E_STRICT, while   "MyFunc('set in function call');"  make  "Fatal error: Only variables should be passed by reference in" without E_STRICT ? would be better to have both at the same error_reporting level, isn't it ?

Btw, it would be good to have it warned in the documentation, probably in the part "III. Language Reference -> 21. References Explained -> Passing by Reference"
 [2007-11-21 16:34 UTC] vrana@php.net
There is a list of values passable be reference in References Explained with the description of behavior of other values: "Any other expression should not be passed by reference, as the result is undefined."
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 27 19:00:03 2025 UTC