|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-01-30 18:00 UTC] robin_fernandes at uk dot ibm dot com
Description:
------------
Passing an undefined variable by reference causes that variable to be implicitly defined. However, this is not the case if the argument is inside parentheses.
This is reproducible on php5.2.5 as well as php5.3 and php6 snaps.
Reproduce code:
---------------
<?php
function f(&$ref) {
$ref='changed';
}
echo "Pass undefined variable by ref:\n";
f( $a );
var_dump($a);
echo "\nPass undefined variable by ref with parentheses:\n";
f( ($b) );
var_dump($b);
?>
Expected result:
----------------
Pass undefined variable by ref:
string(7) "changed"
Pass undefined variable by ref with parentheses:
string(7) "changed"
Actual result:
--------------
Pass undefined variable by ref:
string(7) "changed"
Pass undefined variable by ref with parentheses:
Notice: Undefined variable: b in %s on line 11
Strict Standards: Only variables should be passed by reference in %s on line 11
Notice: Undefined variable: b in %s on line 12
NULL
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 13:00:01 2025 UTC |
Note that if the variable is defined, it is correctly passed by reference whether or not the parens are present: <?php function f(&$ref) { $ref='changed'; } echo "\nPass defined variable by ref with parentheses:\n"; $c = null; f( ($c) ); var_dump($c); ?> Prints: Pass defined variable by ref with parentheses: string(7) "changed"