php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81359 assignement in function parameter
Submitted: 2021-08-14 09:11 UTC Modified: 2021-08-16 10:13 UTC
From: rh at hofercomputing dot ch Assigned: cmb (profile)
Status: Not a bug Package: Compile Failure
PHP Version: 8.0.9 OS: windows
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: rh at hofercomputing dot ch
New email:
PHP Version: OS:

Further comment on this bug is unnecessary.

 

 [2021-08-14 09:11 UTC] rh at hofercomputing dot ch
Description:
------------
function a(&$p){
    $p++;
}

a($x=18);

this produces "Uncaught Error: a(): Argument #1 ($p) cannot be passed by reference in "
but $x is a variable, and this was accepted in recent versions.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-14 11:19 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
fixed version of your code:

$x=18;
a($x);
 [2021-08-14 11:51 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-08-14 11:51 UTC] cmb@php.net
> but $x is a variable, and this was accepted in recent versions.

It was supported, but a notice/strict warning was thrown as of PHP
5.1.2, and the code didn't do what you intended[1], because the
expression `$x=18` actually evaluates to `18`.  As of PHP 8.0.0,
you get an `Error` exception instead.  That is explicitly
mentioned in the migration guide[2]:

| Some "Only variables should be passed by reference" notices have
| been converted to "Argument cannot be passed by reference"
| exception.

And the manual proper states[3]:

| It is an error to pass a value as argument which is supposed to
| be passed by reference.

[1] <https://3v4l.org/Pklt2>
[2] <https://www.php.net/manual/en/migration80.incompatible.php>
[3] <https://www.php.net/manual/en/functions.arguments.php#functions.arguments.by-reference>
 [2021-08-15 11:23 UTC] rh at hofercomputing dot ch
function a(&$p){
    $p++;
}
$x=18;
a($x);
Why do i have to divide in 2 statements? Immediate assignement in a function call is just an elegant way of compact coding. And the interpreter should do exactly this: first: assign the paramter, then call the function. I see no reason not to so!
 [2021-08-15 12:26 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
because it's that way forever and you can't funnily change language behavior just because some random expectations

what you are writing here looks like someone coming from visual basic and it's named params, been there and learnt also the hard way that different programming languages have different behavior

the real problem with this coding style is that you asgin $x for the whole scope and not just that function call

have fun with strtolower(trim(a($somevar=18)) overwriting $somevar for the complete following code and in the worst case it's already used for other purposes

this is simply code smell and bad practice even if it would work and in most usecases references are bad too with no gain (php is COW) but side-effects

-------------------

your biggest problem is that you develop code with supressed errors instead E_ALL because otherwise you would have seen a warning for years - and yes with proper code you can even run production with E_ALL, display_errors disabled and logging enabled to get such things fixed before they become fatal errors
 [2021-08-16 04:09 UTC] a at b dot c dot de
When you declare a function parameter as taking a variable by reference, you have to pass _a variable_. "$x=18" is an assignment expression (not a variable) that gets evaluated to the integer 18, and that integer is passed to the function. Not the variable. The value of the assignment expression is the value on the right-hand side; the left-hand side doesn't get involved.

"a($x=18);" means exactly the same thing as "$x=18; a(18);".
 [2021-08-16 09:16 UTC] rh at hofercomputing dot ch
I am aware that assigning a value to a variable will change that variable, not important if i do it a line before the function call or in the parameterlist. 

If "a($x=18);" means exactly the same thing as "$x=18; a(18);" my wish is
let it be "a($x=18);" means exactly the same thing as "$x=18; a($x); thats all i expect and seams to be logical to me.
 [2021-08-16 09:20 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
> my wish is let it be "a($x=18);"

a programming language with a decades old history and where compatibiliy matters isn't a wishlist - my wish would be that such bad code simply ends in a parse error

both won't happen
 [2021-08-16 10:13 UTC] cmb@php.net
-Block user comment: No +Block user comment: Yes
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 19:01:29 2024 UTC