note 123506 added to language.operators.increment

From: Date: Wed, 09 Jan 2019 19:54:34 +0000
Subject: note 123506 added to language.operators.increment
Groups: php.notes 
Request: Send a blank email to php-notes+get-210028@lists.php.net to get a copy of this message
Please note the difference between post-incrementing in a for-loop and recursive function calls (don't use it in the latter!). <?php $increment = $preIncrement = $postIncrement = 1; echo $increment.' - '.$preIncrement.' - '.$postIncrement; echo '<br>'; echo ($increment+1).' - '.(++$preIncrement).' - '.($postIncrement++); echo '<br>'; echo ($increment+1).' - '.(++$preIncrement).' - '.($postIncrement++); echo '<br>'; echo ($increment+1).' - '.(++$preIncrement).' - '.($postIncrement++); ?> Outputs: 1 - 1 - 1 2 - 2 - 1 2 - 3 - 2 2 - 4 - 3 The for-loop: <?php for($i=0; $i<4; $i++) { echo $i.'<br>'; } ?> Outputs: 0 1 2 3 And the 'headache' mistake, post-increment as parameter of a recursive call: <?php testFunctionNesting(3, 1, 1, 1); function testFunctionNesting($max, $increment, $preIncrement, $postIncrement) { echo $increment.' - '.$preIncrement.' - '.$postIncrement; echo '<br>'; if($increment>=$max) { $inc = $increment; $pre = $preIncrement; $post = $postIncrement; return; } testFunctionNesting($max, ($increment+1), (++$preIncrement), ($postIncrement++)); } ?> Output shows that the postIncremented value is never really available as incremented value: 1 - 1 - 1 2 - 2 - 1 3 - 3 - 1 ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.142 (proxied: 2a02:a443:579c:1:95d4:fe59:969:6a87) ---- Manual Page -- http://php.net/manual/en/language.operators.increment.php Edit -- https://master.php.net/note/edit/123506 Del: integrated -- https://master.php.net/note/delete/123506/integrated Del: useless -- https://master.php.net/note/delete/123506/useless Del: bad code -- https://master.php.net/note/delete/123506/bad+code Del: spam -- https://master.php.net/note/delete/123506/spam Del: non-english -- https://master.php.net/note/delete/123506/non-english Del: in docs -- https://master.php.net/note/delete/123506/in+docs Del: other reasons-- https://master.php.net/note/delete/123506 Reject -- https://master.php.net/note/reject/123506 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#210028) next »