3

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

I've been learning PHP by reading all sorts of things on the web. I've trying to figure out what this is called so I can look it up and read about it and learn how it works. It's the "++" in the code I'm assuming counts up so that the codes knows when to do a new line. I not looking for corrections to this code I'm looking for what the "++" is called so I can look it up.

$i = 0
if (++$i == 10) {
    echo "new line";
}
Community
  • 1
  • 1
Warmour
  • 53
  • 5
  • @animuson Valid point, but he could just as easily have searched for "++operator php" and came up empty. – ppp Feb 28 '12 at 06:43
  • 3
    @AnPel: That's why we close them as duplicates. So people can search, find this, then get directed to the resource that has all this information. Duplicates are kept in-tact as an "alternate route" to the duplicated question. – animuson Feb 28 '12 at 06:45

4 Answers4

5

It's called the prefix increment unary operator. It increments the number or character, then returns its value.

The postfix version does pretty much the same but returns its value first, then increments the variable.

Jakub
  • 19,870
  • 8
  • 62
  • 92
alex
  • 438,662
  • 188
  • 837
  • 957
1

That is a pre-increment. So in that code, $i is first incremented (increased by one), and then its value is used to test for equality to 10.

So the code that you show essentially tests if 1 equals 10 (which it does not.)

Jonathon Reinhart
  • 116,671
  • 27
  • 221
  • 298
1

It is a number operator named the prefix increment. It takes the previous stated number and adds 1 to it. You can find more information about arithmatic operators here: Howtoforge.com

0

It's the pre-increment operator. It will add one to the variable it precedes, and return the result. The post-increment operator returns the current value, and then adds one. The PHP Manuel has an article on all of the operators.

All PHP Operators
Incrementing/Decrementing Operators

Drew Chapin
  • 7,031
  • 5
  • 51
  • 77