#20673 [Ver->Bgs]: Inexplicable arithmetical error due to references
| From: | andi@php.net | Date: | Mon, 09 Dec 2002 12:55:48 +0000 |
| Subject: | #20673 [Ver->Bgs]: Inexplicable arithmetical error due to references | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-27168@lists.php.net to get a copy of this message | ||
ID: 20673
Updated by: andi@php.net
Reported By: dakota@dir.bg
-Status: Verified
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Win 2000 NT/Linux
PHP Version: 4.3.0-dev/4.4.0-dev
New Comment:
Like in most other programming languages you can't use post/pre
increment operators on a variable which is used more than once in an
expression. The result is undefined. We won't print out a warning (like
most other languages).
If you want the result to be 16 then do the following:
$a = 7;
$b =& $a;
$a++;
$a = $a + $a;
echo $a;
//the result is 15;
?>
Previous Comments:
------------------------------------------------------------------------
[2002-11-30 09:28:17] dakota@dir.bg
O.K. but it's PHP - not C. And, if it's wrong, why the parser don't
warn me?
------------------------------------------------------------------------
[2002-11-29 13:11:42] sesser@php.net
never write something like $a = $a + $a++;
if you f.e. try such a construct in C you will get different
results depending on the compiler and/or optimisation level.
------------------------------------------------------------------------
[2002-11-27 07:04:52] dakota@dir.bg
<?
$a = 7;
$a = $a + $a++;
echo $a;
//the result is 14;
?>
When I add a reference to $a, the behavior of $a + $a++ becomes
inexplicable different. Note that $a isn't changed anywhere!
<?
$a = 7;
$b =& $a;
$a = $a + $a++;
echo $a;
//the result is 15;
?>
The only difference is $b =& $a, but why $a takes care of references to
itself?
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=20673&edit=1