$a = 2; $b = & $a; Echo (+ + $a) + ($a + +); Why the answer is 7, not 6.

Source: Internet
Author: User
This post was last edited by Lscxp on 2013-08-29 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 is useless from beginning to end, why is the answer different after adding a line?


Reply to discussion (solution)

Look at this.
http://bbs.csdn.net/topics/390571704

One day two people ask the same question

$b = & $a; The
should turn variable A into a reference type.

$a = 2;
Echo (+ + $a) + ($a + +);
procedure is
to run a $ A self-increment, and then return the result of the increment, that is, 3
($a + +)
$a + + result is 3, and then execute a $ A self-increment, but the result of the expression has returned is 3
so the output is 3+3, A is 4
and
$a = 2;
$b = & $a;
Echo (+ + $a) + ($a + +);
is different, (+ + $a) is not affected,
($a + +) is not the same as before, should be the return value of the $a,
should now be returned to the address of $ A (or, of course, to get the actual value through the address)
returns a $ A address, a $ A increment, a becomes 4, Instead of just getting a $ A address,
now addresses the fetch value, so getting to the 4
result is 3+4

$b = & $a;
The variable a should be converted to a reference type.

$a = 2;
Echo (+ + $a) + ($a + +);
Process is
Run a $ A self-increment and then return the result after the increment is 3
($a + +)
The result of $a + + is 3, and then a $ A increment is executed, but the result of the expression has returned is 3
So the output is 3+3, A is 4.
and
$a = 2;
$b = & $a;
Echo (+ + $a) + ($a + +);
(+ + $a) will not be affected,
($a + +) is not the same as before, it should be the return $a value,
Now it's time to return the address that points to $ A (or get the actual value by address, of course)
When a $ A address is returned, a $ A increment is executed, a becomes 4, and the previous only gets the address of $ A,
Now the address gets the value, so it gets 4.
The result is 3+4.


$a = 2;
$b = & $a;
Echo ($a + +) + (+ + $a); The result is 6.
How do we explain this?

& $a means link so 3+3=6

  • Related Article

    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.