php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #36405 Enhanced expression reduction before function call
Submitted: 2006-02-16 00:33 UTC Modified: 2006-02-16 16:25 UTC
From: thom at genx dot net Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.1.2 OS: Linux (gentoo)
Private report: No CVE-ID: None
 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-02-16 01:15 UTC] pajoye@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

$x = 'foo' is an expression and cannot be passed by reference. Enable E_STRICT to see the message.
 [2006-02-16 01:43 UTC] thom at genx dot net
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
 [2006-02-16 01:58 UTC] pajoye@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

Read the manual, the definition of an expression and a variable are clearly explained. And keep it this bug as bogus it is not a bug (and the behavior is documented).
 [2006-02-16 02:17 UTC] thom at genx dot net
Sorry, I left it as 'Bogus' this time.  I thought that meant it was closed and that any further responses were ignored.

This is changed to a Feature/Change request in hopes that it will receive some attention and/or debate.

Thanks.
 [2006-02-16 07:26 UTC] tony2001@php.net
No, it won't change.
Expressions are expressions, you can't pass them by reference.
 [2006-02-16 16:25 UTC] thom at genx dot net
According to the documentation, PHP supports function arguments that are passed by value (default), reference, value with default, and variable argument lists (> PHP 4).  It does not say that passing by expression is supported.

funcByValue($a++);
funcByValue($a + 9);
funcByValue($a = $b + 1);

I would like to think that PHP will continue to support the current behavior of the above examples because it understands that expressions can be reduced to a more base form.  In the examples above, it is assumed that PHP reduces the expressions to a value and that resulting value is then used in the function call since it supports "passing by value" (and not passing by expression).

With that in mind, an assignment expression can be reduced to a variable.  In the more general scenario, expressions whose outer most component (based on the rules of precedence) is of assignment type can be reduced to a variable.  A variable is the required form for "passing by reference".

Yes, an expression is an expression as you so nicely stated, but they can be reduced.  If expressions are reduced to a value so they can be passed by value, why can't an assigment expression be reduced to a variable so it can be passed by reference?

I understand that there is a workaround, but please do acknowledge the fact that expressions work in the case of passing by value because they are first reduced to a value (and continued support is planned).  In the case of an assignment expression and passing by reference, it would require applying another rule of reduction and may still yield a consistent interface within PHP.

PHP does still consider itself a scripting language and a change like this might be a nuance meant for a programming language, but other changes seem to imply that the PHP development team is open to such suggestions (e.g. argument type hinting).

Thank you for considering this.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC