Who can explain in detail the PHP $a = 10; $b = $a--+--$a; Implementation process?

Source: Internet
Author: User
Who can explain in detail the PHP $a = 10; $b = $a--+--$a; Implementation process?

When I was reading Gaulo's book, "PHP," I Found

$a = 10;$b = $a++ + ++$a;    //书上写的执行过程是$a先自增1后再自增1,再赋给$becho $a;       //$a = 12;echo $b;       //$b = 22;$b = $a-- - --$a;   //$a先自增1后再自增1,再赋给$becho $a;       //$a = 10echo $b;       //$b = 2

If it's logical, then add

$b = $a-- + --$a;echo $a;       //$a = 8;echo $b;       //$b = 18;

Can someone please tell me the detailed execution of this code?

Reply content:

Who can explain in detail the PHP $a = 10; $b = $a--+--$a; Implementation process?

When I was reading Gaulo's book, "PHP," I Found

$a = 10;$b = $a++ + ++$a;    //书上写的执行过程是$a先自增1后再自增1,再赋给$becho $a;       //$a = 12;echo $b;       //$b = 22;$b = $a-- - --$a;   //$a先自增1后再自增1,再赋给$becho $a;       //$a = 10echo $b;       //$b = 2

If it's logical, then add

$b = $a-- + --$a;echo $a;       //$a = 8;echo $b;       //$b = 18;

Can someone please tell me the detailed execution of this code?

Sweat, the three pieces of code are the same, it is necessary to put so much? Whether it is $a++ or not ++$a , you just have to understand that it's better to read the code from left to right.

$a++The value of the first is $a taken out, and then self-added 1, so the $b = $a++; result of the output should be$a=11;$b=10;

++$aThe meaning is to do the $a self-add 1, and then the $a value is taken out, so $b = ++$a; the result of the output should be$a = 11;$b=11;

If you can understand the above two paragraphs, it is not a problem to understand your calculation according to this idea. I'll tell you the last one:

$b = $a-- + --$a;

This should be broken down for easy understanding.

$c = $a--;$d = --$a;$b = $c + $d;

As explained above, the $a-- value is returned here $a , and then the $a self minus 1, at this point $a=9;$c=10; . --$ahere is the $a self minus 1 and then $a the return value, so at this point $a=8;$d=8; . The final result is naturally $a=8;$b=18; .

  • 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.