|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63862 Passing by reference: func($a = $b);
Submitted: 2012-12-27 16:57 UTC Modified: 2013-10-09 07:29 UTC
From: bobwei9 at hotmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: master-Git-2012-12-27 (Git) OS: Mac OS X Mountain Lion
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: bobwei9 at hotmail dot com
New email:
PHP Version: OS:


 [2012-12-27 16:57 UTC] bobwei9 at hotmail dot com
I don't know if this should be considered as a bug or as expected behaviour (=> feature request).

When passing an assignment to a function which expects a variable as reference like sort(), then an E_STRICT message is issued and nothing has changed.
What I want, is that a code like "sort($a = $b);" is executed in the same way as "$a = $b; sort($a);".

This could help to reduce the number of little, unnecessary lines.

Test script:
php -r 'sort($array = [3,1,2]); var_dump($array);'

Expected result:
array(3) {

Actual result:
Strict Standards:  Only variables should be passed by reference in Command line code on line 1
array(3) {


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-31 22:06 UTC] mail+php at requinix dot net
Expected behavior. The "value" of an assignment is just a value and not a 
reference to a variable, just like how "true?$a:$b" is the value of $a and not 
actually the variable $a.
I don't think it would be so difficult to change that behavior but I'm afraid of 
the consequences it might have.

There's also a more subtle change that would be required for your shorthand to 
work. Assignment currently propagates the right-most operand: $a=$b=$c is almost 
equivalent* to $b=$c;$a=$c; and not $b=$c;$a=$b; Unless that changes as well then 
sort($array=[3,1,2]) would try to sort the [3,1,2] and not $array.

* $c is evaluated only once
 [2012-12-31 22:52 UTC] bobwei9 at hotmail dot com
I know how it works now, but I don't know if it is intended (It isn't documented.)

Then I wish that the first variable $a = $b = $c; is passed by array.
 [2013-01-05 05:09 UTC]
-Package: Scripting Engine problem +Package: Documentation problem
 [2013-01-05 05:09 UTC]
$a = $b result a tmp value, not the left variable self.

I wrote a simple explaination for this before, (however it's in chinese)

change to doc problem instead
 [2013-01-05 10:35 UTC] bobwei9 at hotmail dot com
Yeah, it is primarily not documented, but is there a reason why not to change 
behavior? I don't think that it'll introduce a BC break, because it makes today 
no sense to write such a code. (Throws E_STRICT error)

I mean: if there is a $variable = on the beginning of an argument and the 
argument expects a reference, then evaluate first the part after the first =, 
assign the result to the $variable and pas this variable by reference.
Or requires this minor change to be a rfc...?
 [2013-01-05 10:35 UTC] bobwei9 at hotmail dot com
-Type: Bug +Type: Feature/Change Request
 [2013-10-09 07:20 UTC]
-Status: Open +Status: Not a bug
 [2013-10-09 07:20 UTC]
This is expected behaviour, you cannot use an expression as a reference, you must assign the result of the expression to a variable and pass it by reference.
 [2013-10-09 07:26 UTC]
-Status: Not a bug +Status: Open
 [2013-10-09 07:26 UTC]
This behaviour is documented under the References section:

This is not going to change.
 [2013-10-09 07:29 UTC]
-Status: Open +Status: Not a bug
 [2013-10-09 07:29 UTC]
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Sep 25 04:03:37 2021 UTC