note 97869 added to language.expressions
| From: | whatrevolutionoughtyahoodaughtcalm at osu1 dot php dot net | Date: | Thu, 13 May 2010 05:14:07 +0000 |
| Subject: | note 97869 added to language.expressions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169200@lists.php.net to get a copy of this message | ||
To expand upon previous explainations of pre/post inc/dec, you may use the following arithmetic
proofs:
<?php
/**
* Proof of pre/post increment/decrement operations
* in arithmetic expressions.
*/
$a = 3;
echo '<pre>';
echo 'A)'."\n";
$b = $a;
echo ' 1.) b = a = '.$a."\n";
echo ' 2.) (b * --b) = ';
echo $b * --$b;
echo ' = ('.$b.' * '.$b.') = '.($b * $b)."\n";
echo ' 3.) b = '.$b.', a = '.$a;
echo "\n\n";
echo 'B)'."\n";
$b = $a;
echo ' 1.) b = a = '.$a."\n";
echo ' 2.) (b * b--) = ';
echo $b * $b--;
$z = $y = $b;
echo ' = ('.$b.' * '.++$z.') = '.($b * ++$y)."\n";
echo ' 3.) b = '.$b.', a = '.$a;
echo "\n\n";
echo 'C)'."\n";
$b = $a;
echo ' 1.) b = a = '.$a."\n";
echo ' 2.) (b * ++b) = ';
echo $b * ++$b;
echo ' = ('.$b.' * '.$b.') = '.($b * $b)."\n";
echo ' 3.) b = '.$b.', a = '.$a;
echo "\n\n";
echo 'D)'."\n";
$b = $a;
echo ' 1.) b = a = '.$a."\n";
echo ' 2.) (b * b++) = ';
echo $b * $b++;
$z = $y = $b;
echo ' = ('.$b.' * '.--$z.') = '.($b * --$y)."\n";
echo ' 3.) b = '.$b.', a = '.$a;
echo "\n\n";
echo 'E)'."\n";
$b = $a;
echo ' 1.) b = a = '.$a."\n";
echo ' 2.) (b * b * b++) = ';
echo $b * $b * $b++;
$x = $b;
$z = $y = --$x;
echo ' = ('.$z.' * '.$y.' * '.$x.') = '.($z * $y *
$x)."\n";
echo ' 3.) b = '.$b.', a = '.$a;
echo "\n\n";
echo 'F)'."\n";
$b = $a;
echo ' 1.) b = a = '.$a."\n";
echo ' 2.) (b * b * ++b) = ';
echo $b * $b * ++$b;
$z = $y = $x = $b;
echo ' = ('.--$z.' * '.--$y.' * '.$x.') = '.($z * $y *
$x)."\n";
echo ' 3.) b = '.$b.', a = '.$a;
echo "\n\n";
echo '</pre>';
?>
From which I receive these results:
A)
1.) b = a = 3
2.) (b * --b) = 4 = (2 * 2) = 4
3.) b = 2, a = 3
B)
1.) b = a = 3
2.) (b * b--) = 6 = (2 * 3) = 6
3.) b = 2, a = 3
C)
1.) b = a = 3
2.) (b * ++b) = 16 = (4 * 4) = 16
3.) b = 4, a = 3
D)
1.) b = a = 3
2.) (b * b++) = 12 = (4 * 3) = 12
3.) b = 4, a = 3
E)
1.) b = a = 3
2.) (b * b * b++) = 27 = (3 * 3 * 3) = 27
3.) b = 4, a = 3
F)
1.) b = a = 3
2.) (b * b * ++b) = 36 = (3 * 3 * 4) = 36
3.) b = 4, a = 3
This further shows what is meant in http://us3.php.net/manual/en/language.expressions.php#77291
and http://us3.php.net/manual/en/language.expressions.php#74308
where they say, "it doesn't necessarily state that it should happen AFTER an evaluation is
over". However, why proof (D) and proof (E) behave differently, I am unsure right now.
----
Server IP: 192.41.40.227
Probable Submitter: 69.135.215.192
----
Manual Page -- http://www.php.net/manual/en/language.expressions.php
Edit -- https://master.php.net/note/edit/97869
Del: integrated -- https://master.php.net/note/delete/97869/integrated
Del: useless -- https://master.php.net/note/delete/97869/useless
Del: bad code -- https://master.php.net/note/delete/97869/bad+code
Del: spam -- https://master.php.net/note/delete/97869/spam
Del: non-english -- https://master.php.net/note/delete/97869/non-english
Del: in docs -- https://master.php.net/note/delete/97869/in+docs
Del: other reasons-- https://master.php.net/note/delete/97869
Reject -- https://master.php.net/note/reject/97869
Search -- https://master.php.net/manage/user-notes.php