php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53786 about "++" or "--"
Submitted: 2011-01-19 11:40 UTC Modified: 2011-02-10 13:43 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: fx4084 at gmail dot com Assigned: vrana (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.2SVN-2011-01-19 (snap) OS: WIN7
Private report: No CVE-ID: None
 [2011-01-19 11:40 UTC] fx4084 at gmail dot com
Description:
------------
PHP programe like:
<?php
$a = 1,$b=1;
$a = $a + $a++;
$b = $b + $b + $b++;

echo $a.' '.$b;
//print "3 3";
?>

But the same programe in JavaScript or JAVA or C, it's print "2 3";

Test script:
---------------
<?php
$a = 1,$b=1;
$a = $a + $a++;
$b = $b + $b + $b++;

echo $a.' '.$b;
//print "3 3";
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-19 11:43 UTC] fx4084 at gmail dot com
And more example:
<?php
$a = 1,$b = 1,$c=1;
$x = $a + 0 + $a++; //$x = 2;
$y = 0 + $b + %b++; //$y = 3;
//but
$z = $c + $c++; //$c = 3
?>


?>
 [2011-01-19 13:18 UTC] derick@php.net
-Package: *General Issues +Package: Documentation problem
 [2011-01-19 13:18 UTC] derick@php.net
Mixing ++ and + in the same statement provides undefined behaviour in many languages, including C— depending on optimisation etc. In PHP, this also provides undefined behaviour. I'm marking this as a doc issue, as I can't find anything on http://uk.php.net/manual/en/language.operators.increment.php about this.
 [2011-01-19 14:44 UTC] fx4084 at gmail dot com
replay : derick@php.net

example:
<?php

$a=1;
$c=1;
$a = $a + $a++ + ++$a;     //now $a=6; Why? It is right? How it is interpreted?
$c = $c + 0 + $c++ + ++$c; //now $c=5; Why difference between $a and $c?

//In C language or JavaScript a=5;c=5;
?>
 [2011-01-19 16:47 UTC] rasmus@php.net
Like Derick said, this is undefined behaviour.  Not just in PHP but in most 
languages.  Read this about similar undefined behaviours in C, for example:

http://c-faq.com/expr/seqpoints.html

Asking for an explanation of undefined behaviour is non-sensical.
 [2011-01-19 17:02 UTC] fx4084 at gmail dot com
Thanks for answers!
 [2011-02-10 13:42 UTC] vrana@php.net
Automatic comment from SVN on behalf of vrana
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=308186
Log: Don't mix + and ++ (doc bug #53786)
 [2011-02-10 13:43 UTC] vrana@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: vrana
 [2011-02-10 13:43 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I've added an example on the Operator Precedence page.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 22:01:31 2024 UTC