note 95556 deleted from language.operators.increment by danbrown
| From: | danbrown@php.net | Date: | Fri, 08 Jan 2010 22:18:41 +0000 |
| Subject: | note 95556 deleted from language.operators.increment by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164796@lists.php.net to get a copy of this message | ||
Note Submitter: rupertg99 at yahoo dot com
----
daevid:
You need to think of the ++ as an assignment operator. All of your examples are working exactly as
they should, which is the same way this operator has worked in every programming language for
decades.
<?php
$num = $num++; //This returns $num then increments $num (Post-increment).
$num = ++$num; //This increments $num then returns the new $num (Pre-increment).
?>
$num++ does not mean "One more than the value of $num". If that's what you need, you
want $num+1.
$num++ means "Give me $num, then add one to its value".
++$num means "Add one to $num, then give me the new value".
If you simply want to increase $num by 1, you have many choices:
<?php
//All of these are equivalent
$num++;
++$num;
$num += 1;
$num = $num + 1;
?>