php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34795 Passing assignment result by reference passes it by value
Submitted: 2005-10-09 20:58 UTC Modified: 2005-10-20 16:45 UTC
From: tomas_matousek at hotmail dot com Assigned: dmitry (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2005-10-09 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tomas_matousek at hotmail dot com
New email:
PHP Version: OS:

 

 [2005-10-09 20:58 UTC] tomas_matousek at hotmail dot com
Description:
------------
Let's have a function with a parameter passsed by reference.

function f(&$x) { }

Now, call this function as follows:

f($a = $b = $c);

This will sometimes fail sometimes not depending on whether any of the variables $a, $b, $c has previously been used with =& operator. In a such case, the call succeeds with $a being aliased to $x. Otherwise, fatal error occures: "Only variables can be passed by reference".

this code works:

$b =& $z;
f($a = $b = $c);

this code doesn't:

f($a = $b = $c);

So the "correctness" of the code depends on whether there exists an alias of one of the variable. That's weird, isn't it?



Reproduce code:
---------------
$b =& $z;
f($a = $b = $c);

f($u = $v = $w);


Expected result:
----------------
OK. 
OK.

- or -

Error.
Error.


Actual result:
--------------
OK.
Error.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-09 21:11 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-10-09 21:58 UTC] tomas_matousek at hotmail dot com
Well, the versino in CVS doesn't report an error, however it passes by value instead which is imho also incorrect. 
Example:

function f(&$x)
{
  $x = 10;
}

$a = $b = $c = $d = $u = $v = $w = 1;

$b =& $z;
f($a = $b = $c);

var_dump($a,$b,$c);

f($u = $v = $w);

var_dump($u,$v,$w);

--------

prints:
int(10)
int(1)
int(1)
int(1)
int(1)
int(1)

It should print
int(10)
int(1)
int(1)
int(10)
int(1)
int(1)
 [2005-10-09 22:41 UTC] sniper@php.net
Dmitry, can you check this out?

 [2005-10-12 12:58 UTC] tony2001@php.net
"$u = $v = $w" is an expression, not a variable. 
It's not possible to reference an expression and I don't think it can be fixed at all.
 [2005-10-12 14:05 UTC] tomas_matousek at hotmail dot com
"$x =& $y" is also an expression, isn't it?
And f($x =& $y) works well.
 [2005-10-20 16:45 UTC] iliaa@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

This is expected and documented behaviour.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 17:01:33 2025 UTC