$ A2; $ B & $ a; echo (++ $ a) + ($ a ++); why is the answer 7 not 6 post Last edited by lscxp at 18:18:23
Reference php
$a = 2;$b = &$a;echo (++$a) + ($a++) ;
This is 7.
$a = 2;echo (++$a) + ($a++) ;
The answer is 6.
But that $ B has never been used since the beginning and end. why is the answer different after adding a line?
Reply to discussion (solution)
Read this post
Http://bbs.csdn.net/topics/390571704
Two people asked the same question a day.
$ B = & $;
It should be to change variable a to the reference type.
$ A = 2;
Echo (++ $ a) + ($ a ++ );
The process is
Run $ a auto-increment first, and then return the auto-increment result, which is 3.
3 + ($ a ++)
The result of $ a ++ is 3, and then $ a auto-increment is executed, but 3 is returned for the expression.
So the output is 3 + 3, and a is 4.
While
$ A = 2;
$ B = & $;
Echo (++ $ a) + ($ a ++ );
The difference is that (++ $ a) is not affected,
($ A ++) is different from the previous one. it should have returned the value of $,
Now we should return the address pointing to $ a (of course, we need to get the actual value through the address)
After the $ a address is returned, $ a is automatically incremented, and a is changed to 4. Previously, only the address of $ a is obtained,
Now the addressing function is used to obtain the value, so the obtained value is 4.
The result is 3 + 4.
$ B = & $;
It should be to change variable a to the reference type.
$ A = 2;
Echo (++ $ a) + ($ a ++ );
The process is
Run $ a auto-increment first, and then return the auto-increment result, which is 3.
3 + ($ a ++)
The result of $ a ++ is 3, and then $ a auto-increment is executed, but 3 is returned for the expression.
So the output is 3 + 3, and a is 4.
While
$ A = 2;
$ B = & $;
Echo (++ $ a) + ($ a ++ );
The difference is that (++ $ a) is not affected,
($ A ++) is different from the previous one. it should have returned the value of $,
Now we should return the address pointing to $ a (of course, we need to get the actual value through the address)
After the $ a address is returned, $ a is automatically incremented, and a is changed to 4. Previously, only the address of $ a is obtained,
Now the addressing function is used to obtain the value, so the obtained value is 4.
The result is 3 + 4.
$ A = 2;
$ B = & $;
Echo ($ a ++) + (++ $ a); // The result is 6.
How can this be explained?
& $ A indicates that the link is 3 + 3 = 6