3

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.

2
  • thx all guys, understand how it works now Commented May 18, 2016 at 16:05
  • 1
    Welcome and go ahead... Commented May 18, 2016 at 16:09

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

$a = $a++; is redundant and not at all the same thing as $a[0]++ all you need is $a++ no reason to assign it..
3

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.

Comments

2

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

Comments

2

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

5 Comments

It increments after returning $a[0] to the assignment operator, not after the echo. The echo is in the next line of code. Increment takes place before that line of code.
increment never takes place because the variable is reassigned as soon as it's incremented.
Then, it must be a typo. Your statement as I see it on my screen is "it will increment after the echo." It will increment BEFORE the echo, before the assignment, and be overwritten by the assignment operation.
"Increment never takes place" is also incorrect. Increment takes place before the assignment, which overwrites the increment. So, it does take place. It is just overwritten in the next step of operation.
Ive clarified the answer.. yea it was a typo i didnt realize i was saying echo but, yes, it is after and not before, to increment before reassignment you need to put the ++ before the variable, as i explained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.