note 95556 added to language.operators.increment
| From: | rupertg99 at yahoo dot com | Date: | Fri, 08 Jan 2010 22:14:47 +0000 |
| Subject: | note 95556 added to language.operators.increment | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164795@lists.php.net to get a copy of this message | ||
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;
?>
----
Server IP: 64.71.164.2
Probable Submitter: 70.88.157.187
----
Manual Page -- http://www.php.net/manual/en/language.operators.increment.php
Edit -- https://master.php.net/note/edit/95556
Del: integrated -- https://master.php.net/note/delete/95556/integrated
Del: useless -- https://master.php.net/note/delete/95556/useless
Del: bad code -- https://master.php.net/note/delete/95556/bad+code
Del: spam -- https://master.php.net/note/delete/95556/spam
Del: non-english -- https://master.php.net/note/delete/95556/non-english
Del: in docs -- https://master.php.net/note/delete/95556/in+docs
Del: other reasons-- https://master.php.net/note/delete/95556
Reject -- https://master.php.net/note/reject/95556
Search -- https://master.php.net/manage/user-notes.php