Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Pre and post increment - the ++ operator

The ++ (increment) operator in PHP (and Perl, and C, and Ruby) adds one to the value held in a variable and saves the result back in the same variable. But you'll see both $n++; and ++$n; in code. What is the difference?

If you write the ++ in front of the variable name, the variable is incremented before it is used for anything else in the context it is written (know ad a pre-increment), but if it is written after the variable name, the existing contents of the variable are used for anything else that's done in the content and the variable is then increments - a post increment. Look at this example:

<?php
$f = 6 + 7; # 1
$k = $f++; # 2
$m = ++$f; # 3
$g = 8 + 9;
$h = $f . $g; # 4
print "$h $k $m\n";
?>


The line #1 sets the variable $f to 13.

Line #2 sets $k to the contents of $f (13) and THEN increments $f to make it 14. A postincrement.

Line #3 increments $f and THEN stores the result (now 15) into $k. A preincrement

So the code runs like this:

Dorothy:f9php grahamellis$ php silly
1517 13 15
Dorothy:f9php grahamellis$


Some Notes

1. In C, if you use ++ on a pointer variable, it will increment it by one memory address and thus provide you with a way of incrementing through an array. If you write

int * mypointer;
...
mypointer++;


You will actually add two or four to the variable, depending on whether you are compiling with 2 byte or 4 byte integers (the latter is he default in most systems these days)

2. The -- operator (decrement) behaves in the same way

3. As good programming practise in code that you're writing for maintainance by casual or inexperienced programmers, you should consider writing the assignment and increment as two separate operations - for example, code a postincrement as:

$m = $f; $f++

4. If you want to increment by a different amount, use the += operator in any of the languages that I have mentioned so far:

$days += 5;

5. Python does NOT support the ++ operator, but it does support +=

6. You will find that most languages that we teach also support -=, *=, /=, %= ... and even more similar operations. Here's a Perl example:

$agemlimit ||=18;

Which sets $agelimit to 18 unless it already has a non-zero value, Neat, huh, but pity the newcomer!

Line #4 shows how the . operator concatenates strings in PHP ... even if the incoming variables are both integers. The type conversion is automatic.
(written 2009-02-03)

 
Associated topics are indexed under
Q101 - Object Orientation and General technical topics - Programming Principles
  [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
  [3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
  [3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
  [2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
  [2769] Easy - but for whom? - (2010-05-18)
  [2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2510] The music of the stock market - (2009-11-22)
  [2415] Variable names like i and j - why? - (2009-09-22)
  [2327] Planning! - (2009-08-08)
  [2310] Learning to write high quality code in Lua - (2009-07-30)
  [2228] Where do I start when writing a program? - (2009-06-11)
  [2001] I have not programmed before, and need to learn - (2009-01-19)

P258 - Perl - Solaris, OSX and Linux
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [1702] Running operating system commands in Perl - (2008-07-08)
  [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)


Back to
Short health and safety course
Previous and next
or
Horse's mouth home
Forward to
sw_vers - what version of OSX am I running?
Some other Articles
Melksham Climate Friendly Group
Injection Attack if register_globals in on - PHP
Carry on Training - in spite of the weather
sw_vers - what version of OSX am I running?
Pre and post increment - the ++ operator
Short health and safety course
Learning Python - many new example programs
Baby Caleb and Fortune City in your web logs?
UnboundLocalError - Python Message
Python - a truly dynamic language
3603 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2012: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2022_Pre ... rator.html • PAGE BUILT: Fri Feb 3 14:16:04 2012 • BUILD SYSTEM: wizard