php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62778 pre-increment operator produces rvalue rather than lvalue
Submitted: 2012-08-08 11:50 UTC Modified: 2019-09-01 04:22 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: dexen dot devries at gmail dot com Assigned:
Status: No Feedback Package: Variables related
PHP Version: 5.4.6RC1 OS:
Private report: No CVE-ID: None
 [2012-08-08 11:50 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-21 13:20 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2019-08-21 13:20 UTC] nikic@php.net
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.
 [2019-09-01 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 13 03:01:33 2024 UTC