$ A = 2; $ B = & $ a; echo (++ $ a) + ($ a ++); why 7 is not 6?

Source: Internet
Author: User
$ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.