Req #62778 [Opn->Fbk]: pre-increment operator produces rvalue rather than lvalue
| From: | nikic@php.net | Date: | Wed, 21 Aug 2019 13:20:28 +0000 |
| Subject: | Req #62778 [Opn->Fbk]: pre-increment operator produces rvalue rather than lvalue | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222340@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62778&edit=1
ID: 62778
Updated by: nikic@php.net
Reported by: dexen dot devries at gmail dot com
Summary: pre-increment operator produces rvalue rather than
lvalue
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: Variables related
PHP Version: 5.4.6RC1
Block user comment: N
Private report: N
New Comment:
Is there any motivation for allowing this? PHP is pretty consistent about not returning references
from assignment-like operations -- the only one that does so is the proper by-reference assignment
(=&). I don't see any reason why we would want to change this.
Previous Comments:
------------------------------------------------------------------------
[2012-08-08 11:50:17] dexen dot devries at gmail dot com
Description:
------------
Result of pre-increment (also pre-decrement) operator is a value (
rvalue') rather than a
reference to variable (lvalue'). This prevents assigning result of the operator to
reference, or passing it by reference.
There is no apparent reason for such limitation; the operator only applies to variables (never to
other expressions), and PHP core supports references.
The requested behavior is similar to C++ (result of pre-increment operator is reference); the
current behavior mimics C (result of pre-increment operator is an rvalue).
Test script:
---------------
$a = 0;
$b = 0;
$c = 0;
function foo(&$v) {
$v = $v + 1;
}
++$a;
foo($a);
var_dump($a);
foo(++$b);
var_dump($b);
$b =& ++$c;
var_dump($c);
Expected result:
----------------
$a === 2
$b === 2
$c === 1
Actual result:
--------------
$a === 2
Strict Standards: Only variables should be passed by reference
$b === 1
PHP Parse error: syntax error, unexpected '++' (T_INC)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62778&edit=1