What's the Difference Between ++$I and $I++ in PHP

What's the difference between ++$i and $i++ in PHP?

++$i is pre-increment whilst $i++ post-increment.

  • pre-increment: increment variable i first and then de-reference.
  • post-increment: de-reference and then increment i

"Take advantage of the fact that PHP
allows you to post-increment ($i++)
and pre-increment (++$i). The meaning
is the same as long as you are not
writing anything like $j = $i++,
however pre-incrementing is almost 10%
faster, which means that you should
switch from post- to pre-incrementing
when you have the opportunity,
especially in tight loops and
especially if you're pedantic about
micro-optimisations!"
- TuxRadar

For further clarification, post-incrementation in PHP has been documented as storing a temporary variable which attributes to this 10% overhead vs. pre-incrementation.

Difference $i++ and ++$i when 0

post increment vs pre increment.

Post: Trailing $i++ means that $i is returned and then incremented after.

Pre: Preceding ++$i means that $i is incremented and that result is returned.

So $x is set to 0 (the initial value of $i) and then incremented. $i is now equal to 1. Then $i is incremented again to 2 and that value is set in $y. So in the end, $x=0 and $y=2 and $i=2. Your code could be rewritten as:

$i=0;
//x, post increment, set x to i then increment after.
$x=$i;
$i=$i+1;

//y, pre increment, increment first and then set y to i.
$i=$i+1;
$y=$i;

Same thing applies to the decrement operator --$i and $i--.

is there a reason to use ++$i in for loop?

++$i is a micro-optimisation, it executes fractionally faster than $i++. However, unless the $subusers array is being changed within the loop so that count($subusers) can change from one iteration to the next, then any slight positive gain in speed is being negated (and then some) by counting the number of array entries every iteration.

Note that $i++ and ++$i would both execute at the end of each iteration of the loop. It isn't the same as initialising $i to 1 rather than to 0.

What is the difference between .= and += in PHP?

Quite simply, "+=" is a numeric operator and ".=" is a string operator. Consider this example:

$a = 'this is a ';
$a += 'test';

This is like writing:

$a = 'this' + 'test';

The "+" or "+=" operator first converts the values to integers (and all strings evaluate to zero when cast to ints) and then adds them, so you get 0.

If you do this:

$a = 10;
$a .= 5;

This is the same as writing:

$a = 10 . 5;

Since the "." operator is a string operator, it first converts the values to strings; and since "." means "concatenate," the result is the string "105".

PHP: What's the difference between initializing an array with new vs without it?

You don't instantiate an array in PHP using:

$foo=new array(); // error in PHP

That's for Javascript:

foo=new Array(); // no error in Javascript

In PHP, new is used only for instantiating objects.

In general, ++ is the same thing as +1. Why ++ != +1 on this code?

There is a basic difference between $i++ and ++$i.

pre-increment

++$i is Pre-increment, Increments $i by one, then returns $i

post-increment

$i++ is Post-increment, Returns $i, then increments $i by one

Addition

$i += 1 is Addition, adds 1, then return $i

Please have a look: https://www.php.net/manual/en/language.operators.increment.php

In your case use

$val['id']++; 

instead of

$val['id'] = $val['id']++;

Hope, it helps.

Pre-incrementation vs. post-incrementation

Pre- or post-incrementing do not magically delay things until later. It's simply inline shorthand.

Sample Image

// pre-increment
$var = 5;
print(++$var); // increments first, then passes value (now 6) to print()

// post-increment
$var = 5;
print($var++); // passes value (still 5) to print(), then increments

Now let's look at a loop.

for ($i = 0; $i < 9; $i++) {
print($i);
}

The last part of the loop declaration (the $i++) is simply the statement to execute after each time through the loop. It "passes" the value to nowhere, then increments it. $i isn't used anywhere at that time. Later when the next statement is executed (print($i);), the value of $i has already increased.

// add 1, then do nothing with $i
for ($i = 0; $i < 9; ++$i) {}

// do nothing with $i, then add 1
for ($i = 0; $i < 9; $i++) {}

Whichever way you do it, $i will be the same within the loop.


If it helps, you can think of them as small routines that kind of do this:

// ++$i
{
$i = $i + 1;
return $i;
}

// $i++
{
return $i;
$i = $i + 1;
}

As I reread your question, I think the confusion is more with how the loop works than how increment operators work. Keeping in mind that the increment is a straightforward, all-at-once operation, here's how third expression in the loop works.

// here's a basic loop
for ($i = 0; $i < 9; $i++) {
// do loop stuff
print($i);
}

// this is exactly what happens
for ($i = 0; $i < 9; ) {
// do loop stuff
print($i);

$i++;
}

Just because that last line can be put in the loop declaration doesn't give it any special powers. There are no references or anything used behind the scenes. The same $i variable is seen both inside and outside the loop. Every statement inside or outside the loop directly looks up the value of $i when necessary. That's it. No funny business.



Related Topics



Leave a reply



Submit