Req #81359 [Nab]: assignement in function parameter
| From: | rh at hofercomputing dot ch | Date: | Sun, 15 Aug 2021 11:23:26 +0000 |
| Subject: | Req #81359 [Nab]: assignement in function parameter | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-235828@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81359&edit=1
ID: 81359
User updated by: rh at hofercomputing dot ch
Reported by: rh at hofercomputing dot ch
Summary: assignement in function parameter
Status: Not a bug
Type: Feature/Change Request
Package: Compile Failure
Operating System: windows
PHP Version: 8.0.9
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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!
Previous Comments:
------------------------------------------------------------------------
[2021-08-14 11:51:27] 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-14 11:19:34] rtrtrtrtrt at dfdfdfdf dot dfd
fixed version of your code:
$x=18;
a($x);
------------------------------------------------------------------------
[2021-08-14 09:11:49] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81359&edit=1