php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43984 Extra parentheses prevent implicit variable initialisation when passing by ref
Submitted: 2008-01-30 18:00 UTC Modified: 2008-01-30 21:06 UTC
From: robin_fernandes at uk dot ibm dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.5 OS: Windows
Private report: No CVE-ID: None
 [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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-30 18:01 UTC] robin_fernandes at uk dot ibm dot com
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"
 [2008-01-30 21:06 UTC] derick@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

You\'re passing an expression \"($b)\" as a variable. An expression is not a variable. For this to work, you need to pass a variable only \"$b\".
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Sep 15 09:00:02 2025 UTC