Bug #53786 [Opn]: about "++" or "--"

From: Date: Wed, 19 Jan 2011 16:02:30 +0000
Subject: Bug #53786 [Opn]: about "++" or "--"
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-5840@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=53786&edit=1 ID: 53786 User updated by: fx4084 at gmail dot com Reported by: fx4084 at gmail dot com Summary: about "++" or "--" Status: Open Type: Bug Package: Documentation problem Operating System: WIN7 PHP Version: 5.2SVN-2011-01-19 (snap) Block user comment: N Private report: N New Comment: Thanks for answers! Previous Comments: ------------------------------------------------------------------------ [2011-01-19 16:47:07] 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 14:44:31] 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 13:18:22] 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 11:43:52] 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 11:40:10] 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"; ?> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=53786&edit=1

« previous php.doc.bugs (#5840) next »