NZT
NZT

Reputation: 53

PHP increment feature

Can someone please explain why this happens:

$a[0] = 1;
$a[0] = $a[0]++;
echo $a[0];

In this code, a[0] always becomes 1. Even if $a[0] = $a[0]++; is done multiple times it does not increment the value of a[0].

but if we assign to a different variable like this:

$a[0] = 1;
$b[0] = $a[0]++;
echo $a[0];

$a[0] will be set to 2. (And of course b[0] will be 1).

I cannot understand why this happens.

Upvotes: 3

Views: 159

Answers (4)

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

Beacuse your ++ is after the variable, which means it will increment after the ++ operator has returned the original value and the assinment operator reassigns it back to the original value.

Since you are reassigning the variable before the increment, the number never has a chance to increment..

The proper way to do it is to just drop the reassignment..

$a[0] = 1;
$a[0]++;
echo $a[0];

https://3v4l.org/Tijrb

You could also move the ++ to the start and it will behave as expected:

$a[0] = 1;
$a[0] = ++$a[0];
echo $a[0];

https://3v4l.org/9fZ3U

Upvotes: 2

Murad Hasan
Murad Hasan

Reputation: 9583

Okey, Lets start with first one. You assign the value of $a[0] as 1, you apply an increment operation on it, but store again in $a[0], The $a[0] is not update yet cause the increment is post-increment. But if you did it as pre-increment then you will get the value 2.

Ex: 1

$a[0] = 1;
$a[0] = $a[0]++;
echo $a[0]; //1

See the effect of pre-increment:

$a[0] = 1;
$a[0] = ++$a[0];
echo $a[0]; //2

Ex:2

Same as example one, you did the post-increment, this time you store in different variable that means, the $a[0] not updated here and the increment operation implement. so you got the result as 2. Here the post and pre both is same.

$a[0] = 1;
$b[0] = $a[0]++;
echo $a[0]; //2

Here the value of $b[0] will be same as the value of $a[0] at this stage. But if the pre-increment applied here then the value of $b[0] also changed and its stores 2.

Note: All you have to understand the pre-increment and post-increment. For more visit - language.operators.increment.php

Upvotes: 2

kainaw
kainaw

Reputation: 4334

Simplify this to remove the index. It is not needed.

$a = $a++;

First, the right side is executed. Because the ++ is after the variable, it says "return $a and then increment $a." It does exactly that. It returns $a to the assignment operation and then increments $a.

After the right side is executed, the assignment operation runs. The right side returned $a before it was incremented. So, it is still the original value of $a. That is what $a is assigned to. This overwrites the increment operation that just took place on the right side.

Upvotes: 3

iainn
iainn

Reputation: 17417

From the PHP documentation:

PHP supports C-style pre- and post-increment and decrement operators.

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

(see http://php.net/manual/en/language.operators.increment.php)

So when using post-increment ($x++), if you assign the result to another variable, you'll end up with the value of the variable before the increment has taken place.

Upvotes: 3

Related Questions