0

Possible Duplicate:
Understanding Incrementing
Reference - What does this symbol mean in PHP?

What does the ++ means, I have seen this also in javascript

$this->instance = ++self::$instances;

Best Regards

Community
  • 1
  • 1
Uffo
  • 9,628
  • 24
  • 90
  • 154
  • 2
    http://php.net/manual/en/language.operators.increment.php – Book Of Zeus Dec 16 '11 at 18:28
  • See also: [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) Although this is a very strange case, that may need more explanation. Where'd you get this code from? – Wesley Murch Dec 16 '11 at 18:29
  • I can't help but wonder sometimes if people think to themselves, "Hey I could probably google this and get an instantaneous answer. But screw it; that would be way too easy." –  Dec 16 '11 at 18:45
  • http://ch2.php.net/language.operators.increment – djot Dec 16 '11 at 18:31

3 Answers3

8

The PHP documentation is quite helpful here:

Example     Name               Effect
-----------------------------------------------------------------------
++$a        Pre-increment      Increments $a by one, then returns $a.
$a++        Post-increment     Returns $a, then increments $a by one.

Your code is equivalent to this:

self::$instances = self::$instances + 1;
$this->instance = self::$instances;
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Um, no it isn't... It's the same as `$this->instance = self::$instances = self::$instances + 1`. – lonesomeday Dec 16 '11 at 18:30
  • ++foo is NOT the same as foo++. ++foo: you increment before the other operations, foo++: you increment after the other operations. Try bar = ++foo * 3 and bar = foo++ * 3 (with foo any number). – Hidde Dec 16 '11 at 18:31
  • Sorry, edits don't show up instantly on SO. No idea what I was thinking while I wrote the first version. – Blender Dec 16 '11 at 18:32
  • 2
    Woah, you mean I too can get sweet up-votes for answering really basic questions!?!? I gotta look into this :) –  Dec 16 '11 at 18:42
  • I actually have 7 accounts and upvote every answer I post seven times. So far nobody has caught me... – Blender Dec 16 '11 at 18:52
  • @ Blender - you got to be kidding! – D. Rattansingh Dec 16 '11 at 18:57
  • @Blender *lmao* I'm still waiting for someone to actually ASK a question on SO about how to use bot-controlled accounts with cURL to automate up-voting of their own posts. Awesome. –  Dec 16 '11 at 19:27
0

It's a pre-increment - http://php.net/manual/en/language.operators.increment.php

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
0

$x is hardcoded here as 10 but could easily be some integer value inputted by the user.

<?php    
    $x=10;
    $count=0;
    while($count<=10)
    {
        printf("<br/>%d", $x++);
        $count++;
    }// end while    
?>

// prints out from 10 to 20.

See the $x++, it means use the value of x then increment by 1 (++ --> x=x+1). So we print out x which is 10, increment by 1 and the loop loops, print out 11 increment by 1 etc. Now if we have ++$x, then we would increment first and then print out the value. So the same code above with ++$x would print out from 11-21 since when we initially enter the loop and x=10, it is incremented to 11 and then printed.

See the $count++;, the very same concept. I used this as a counter to have the while loop loop 10 times exactly. IT's equivalent to count=count+1; While putting the ++ on the left or right for $x did matter, for the count it doesnt matter since we're not using count or printing it out. So if I had ++$count in the above code it would execute the exact same.

D. Rattansingh
  • 1,569
  • 3
  • 19
  • 30