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
}
instead of :
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++ ;
}
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.