|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-02-16 00:33 UTC] thom at genx dot net
Description:
------------
Inline assignment of a variable used as an argument to a function (passed by reference) does not behave properly. Earlier versions of PHP (< 5.1.2) and other programming languages recognize the assignment before the function call is made. It seems that PHP 5.1.2 recognizes the assignment after the function call is made.
Reproduce code:
---------------
<?php
function testAppend(&$string)
{
$string .= 'testAppend';
}
testAppend($x = 'foo');
echo $x;
?>
Expected result:
----------------
footestAppend
Actual result:
--------------
foo
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Fri Jun 19 06:00:02 2026 UTC |
Maybe this is not a good argument, but other languages still interpret that as passing $x by reference, but to do the assigment first. I am going to use C++ as an example (since PHP has tried to model some of its behavior from): #include <iostream> using namespace std; void foo(int &x) { x = 9; } int main() { int x; foo(x = 1); cout << x << "\n"; } The output is: 9 There are no compiler warnings or errors (at the highest reporting level). I understand that this is not C++, but previous versions of PHP (< 5.1.2) behaved consistently with other programming languages in the way that inline assignments were handled. Is it something the PHP development team would consider (reverting back to a more consistent behavior)? Thanks, thom