# Increments, ++\$a and \$a++

I can’t figure this out…

They say ++X is a PRE increment,
and X++ is a POST increment,

But a PRE/POST on what? The output? Or what am i saying now… What does any of this mean?

Can someone show me a example of using ++\$a vs \$a++ where it WOULD MATTER?

Here is some stuff i was testing

``````<?php

\$a = 2;
\$a = \$a- + 1; // It equals 1, why?
echo \$a;

\$b = 1;
\$b++;
echo \$b; // 2

\$c = 1;
++\$c;
echo \$c; // 2

\$d = 1;
--\$d;
echo \$d; // 0

\$e = 1;
\$e--;
echo \$e; // 0

echo '<hr />';

for (\$i = 0; \$i < 5; \$i++)
echo \$i; // 01234

echo '<hr />';

for (\$i = 0; \$i < 5; ++\$i)
echo \$i; // 01234

echo '<hr />';

``````

Okay this kind of makes sense, I find it a little tricky :\

Trying to wrap my head around this:
The pre/post increment only makes a difference if the increment is incorporated inside another statement and determines whether the pre or post increment value will be used in that other statemen

Say you are stuck with a startup value for a loop and that value is 0 (such the first key a new array defaults to).

Lets say that your requirement is to echo out the number 1 in the first part of the loop, because having a human readable array makes more sense to people when it starts with 1.

``````
\$element_out_of_your_control = 0 ;

foreach( \$rows as \$row ) {
echo ++\$element_out_of_your_control ;
// do stuff

}

``````

``````
foreach( \$rows as \$row ) {
\$element_out_of_your_control++ ;
echo \$element_out_of_your_control ;
// do stuff

}

``````

using ++\$a increments it immediately, whereas \$a++ shows the increment next time it is used, hence you often see it as the last line inside a loop.

``````
\$ctr = 1 ; // set your counter

foreach( \$rows as \$row ){

// do stuff

\$ctr++ ;
}

``````

It’s PRE/POST on the current statement. Doing the following yields different results:

``````\$a = 1;
echo ++\$a;

\$b = 1;
echo \$b++;
``````

As for where it would matter is very generic. Maybe you want to echo a counter then increment it by one. So instead of doing:

``````echo \$a;
++\$a;
``````

You could just do:

`````` echo \$a++;
``````

Most of the time you’ll just use the PRE increment, as this avoids the parser having to create a copy of the value before incrementing.

This is basic math, but it may make more sense if you space it consistently. What you wrote is identical to this:

``````<?php

\$a = 2;
\$a = \$a - +1; //which is the same as \$a = \$a - 1; and \$a=\$a-1;
echo \$a;
``````

The +1 just reassures that the 1 is positive, which is always assumed and therefore unneeded.

Think of it this way:

PRE: Increments the variable immediately.
POST: Increments the variable after it has been used.

It is one of those things you should not dwell upon - but simply acccept.

Like zits, grey hairs and weight gain.

\$a = 5;
\$b = ++\$a;

\$a = 5;
\$c = \$a++;

In both cases \$a ends up equal to 6. The pre/post increment affects the values of \$b and \$c since \$b is assigned the value of \$a after incrementing \$a while \$c is set the value of \$a before it is incremented.

The pre/post increment only makes a difference if the increment is incorporated inside another statement and determines whether the pre or post increment value will be used in that other statement.