php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38013 pass by reference with assignment
Submitted: 2006-07-05 08:33 UTC Modified: 2006-07-05 08:55 UTC
From: flex at city dot ee Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.1.4 OS: Linux
Private report: No CVE-ID: None
 [2006-07-05 08:33 UTC] flex at city dot ee
Description:
------------
When passing in variable which gets assigned
in function call, PHP wrongly thinks it got something
other than variable:

function test(&$a) { $a = 2; } // take $a as reference
test($m = 5); // should mean assign 5 to $m and _then_ pass in variable $m

Instead it looks like php sends scalar 5 to function (and throws E_STRICT error), which is plainly wrong. Or is the documentation wrong about operator precedence?

Really: <?php $m=1;test($m); ?> should mean exactly same as <?php <?php test($m=1); ?>. Currently it does not and IMHO this is very counter-productive to PHP's KISS principle. Especially that last S.

By the way I know there are tons of similar bug reports marked Bogus. But please think about this report once more.

Also you should consider the fact that the 'expected result' shown in this bug report is not hand-written but obtained from PHP 4.4.2 - and this only means serious BC problem.


Reproduce code:
---------------
<?php
function test(&$a) { $a = 2; }

$m = 1;
test($m);
var_dump($m);

test($m = 1);
var_dump($m);
?>


Expected result:
----------------
int(2)
int(2)

Actual result:
--------------
int(2)
int(1)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-05 08:55 UTC] tony2001@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

$m = 5 is an expression, not a variable.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 18:01:26 2025 UTC