PHP increment feature - php

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.

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.

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.

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

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

Related

Does returning "-1" with usort really move the $b variable or does it keep it in the same place?

A simple piece of code written by me:
<?php
function testing($a,$b){
if ($a < $b ){
return -1;
}
elseif ($a > $b){
return 1;
}
//else {
//return 0;
//}
}
$array = array(1,3,2,4,5);
usort($array, "testing");
var_dump($array);
?>
This is from the top comment (highest rated comment and from 5 years ago) on the php.net manual's usort page:
"If you return -1 that moves the $b variable down the array, return 1 moves $b up the array and return 0 keeps $b in the same place."
As far as I was looking at the piece of code that I've written returning "-1" does not move the $b, it stays in the same place. It is only the "return 1;" statement that moves the $b (as compared to the $a, current $a-$b) pair.
Lets say we have something like this:
[1,3],2,4,5 - Return -1
The square brackets indicate the current $a-$b pair. Would we get something like this:
1,2,3,4,5
,meaning the $b would be switched with the following element which is out of the current $a-$b pair?
The point here is that I think that it is only the current $a-$b elements that can get switched. And with this the "return -1;" statement does not do any moving, which is not how I was thinking this works.

Return by reference a increased variable

I know this is simple example, and know why return 21, but dont understand why its working in this way php:
function increase(&$a) {
return $a++; // now i expect $a = 2, but return FIRST the reference (1), an increase later..?
}
$a = 1;
$b = increase($a);
echo $a.$b;
Special behavior with operators and byRef?
$a++ is post-incrementation
the old value of $a is returned and not the incremented value.
but in case of ++$a it is pre-incrementation, the value of $a is incremented and the new value is returned.
So, in this case if $a = 1 and you do $a++ it will return 1, while if you use ++$a it will return 2.
When $a is initially 1, the post-increment operator in the function tells PHP To return 1, and then increment $a to 2.... so the returned value assigned to $b is 1; and the by-reference ensures that the incremented $a is its new value in the global scope.
You're then concatenating $a (now 2 following the post-increment) with $b (1) giving 21

How come the default value of a variable stays the same in the first iteration even after using the ++ operator in a do-while PHP loop?

Let's say I have this following code:
<?php
$i = 1;
$user_login = "some_name";
do {
$user_login_tmp = $user_login . "_" . ($i++);
echo $user_login_tmp . "\n";
} while ($i<10);
?>
As seen in this demo(clickable!), the first echo echoes some_name_1.
This seems kinda weird to me since there is $i++ there.
How come that the first output isn't ..._2? Am I missing something?
I tried looking for an answer in the PHP manual page of the do-while loop but I couldn't find my answer there...
$i++ does post-increment, which returns the variable, then increments by one.
http://php.net/manual/en/language.operators.increment.php
The behaviour you described can be achieved by using pre-increment: ++$i.
$i++ is a post-increment. That means the increment takes place after the value is taken. You can use ++$i for a pre-increment, where the increment takes place before the value is taken.
Generally, most programmers prefer to use a pre-increment except where a post-increment is required. They tend to be slightly cheaper because only one value is required. With a pre-increment, you have to keep the old value around while you do the increment, which makes it slightly more expensive.
$i++ Post-increments, it means: return $i and then increments $i by one.
The demo's output will start with ..._2 if you change the code to (++$i) (new demo)
Check out the PHP.net's page about the incrementing operator:
http://php.net/manual/en/language.operators.increment.php
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.
With $i++, the ++ happens after getting the value of $i. Conversely, ++$i increments first, then "returns" the new value.
$i++ is using the post-operator. After $i is concatenated it is then evaluated.

Confusion over the value of a variable after running a PHP program

I'm studying for my finals and I came across this question:
consider this following PHP code, write the output after executing it
<?php
$a=3;
$b=$a++;
IF($a>$b)
{
echo "a>$b";
}
else if ($a == $b)
{
echo "a=$b";
}
else
{
echo "a < $b";
}
?>
When I output it in my text editor I get a < 3, but I don't understand why though?
I thought a is assigned to 3 and also b is assigned to a++ 3 and 3==3 so should a==3 be printed out?
No, you are using post-increment operator on $a. So, $b will be assigned a value of 3, and later, when the statement is executed, $a will increment itself by one, and become 4. So, you'll now be comparing $a as 4 and $b as 3.
Hence you get the result a > 3
The $a++ incrementation happens after the expression gets evaluated, while ++$a would happen before.
So in your case, $b was first set to 3, and then $a was increased.
$a++ tells the variable $a explicitely to increase, no matter if you assigning to another variable or not!
This gives the possibility to do things like if ($a++ > 10) { // ... in loops.
For your case you would have to take $b = $a + 1;
<?php
$a=3;
$b=$a++;
// $b = 3 and $a = 4 now
IF($a>$b)
{
echo "a>$b";
}
else if ($a == $b)
{
echo "a=$b";
}
else
{
echo "a < $b";
}
?>
I tested your code and I get:
a>3
which makes sense
$a is 3 but is increased to 4 when you do $a++
$b is just $a before the ++ action so it stays 3
Think of $a++ as $a = $a + 1 then it makes sense

Why 1 + decrementing the value + 1 = 2?

I found a piece of code (from one of our developer) and I was wondering why the output of this is 2?
<?php
$a = 1;
$a = $a-- +1;
echo $a;
thanks
I'll give my explanation a whirl. We're talking about a variable referencing some value off in the system.
So when you define $a = 1, you are pointing the variable $a to a value 1 that's off in memory somewhere.
With the second line, you are doing $a = $a-- + 1 so you are creating a new value and setting that to $a. The $a-- retrieves the value of the original $a, which is 1 and adds 1 to make 2 and creates that value somewhere else in memory. So now you have a variable $a which points to 2 and some other value 1 off in memory which along the way decremented to 0, but nothing is pointing at it anymore, so who cares.
Then you echo $a which points to your value of 2.
Edit: Testing Page
$a-- decrements the value after the line executes. To get an answer of 1, you would change it to --$a
<?php
$a = 1;
$a = --$a +1; // Decrement line
echo $a;
?>
What the?
Just to clarify the other answers, what you have going on in this line:
$a = $a-- +1;
Basically when PHP evaluates $a--, it actually returns the value of $a, and then runs the operation of decrementing it.
Try this
$a = 1;
echo $a--; //outputs 1;
echo $a; //outputs 0;
When you run this code, you will see that the number only decrements after it has been returned. So using this logic, it's a bit more clear why
echo $a-- + 1;
would output 2 instead of 1.
A better way
Perhaps a better way, arguably more clear would be
$a = $a -1 + 1
$a = 1; /* $a is 1 */
$a = ($a--) /* returns 1 and decrements the copy of $a */ + 1 /* 1 + 1 = 2 */;
echo $a; /* 2 */
The above is equivalent to something like:
$a = 1; /* $a is 1 */
$temp = $a + 1; /* 1 ($a) + 1 = 2 */
$a = $a - 1; /* decrements $a */
$a = $temp; /* assigns the result of the above operation to $a */
echo $a;
That actually pretty much what PHP translates that into, behind the scenes. So $a-- is not such a useful operation, since $a is going to be overwritten anyway. Better simply replace that with $a - 1, to make it both clearer and to eliminate the extra operation.

Categories