Who can explain in detail the execution process of $ a = 10; $ B = $ a -- + -- $ a; in PHP?

Source: Internet
Author: User
Who can explain in detail the execution process of $ a10; $ B $ a -- + -- $ a; in PHP? When I read Gao luofeng's book "elaborate on PHP", I found {code ...} if the above logic is used, add {code ...} can anyone tell us the detailed execution process of this code? Who can explain in detail the execution process of $ a = 10; $ B = $ a -- + -- $ a; in PHP?

When I read Gao luofeng's book "elaborate on PHP", I found that

$ A = 10; $ B = $ a ++ $ a; // The Execution Process in the book is: $ a First Auto-increment 1 and then auto-increment 1, then assign $ becho $ a; // $ a = 12; echo $ B; // $ B = 22; $ B = $ a ----- $; // $ a first incrementing by 1, then incrementing by 1, and then assigned to $ becho $ a; // $ a = 10 echo $ B; // $ B = 2

If the above logic is used

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

Can anyone tell us the detailed execution process of this code?

Reply content:

Who can explain in detail the execution process of $ a = 10; $ B = $ a -- + -- $ a; in PHP?

When I read Gao luofeng's book "elaborate on PHP", I found that

$ A = 10; $ B = $ a ++ $ a; // The Execution Process in the book is: $ a First Auto-increment 1 and then auto-increment 1, then assign $ becho $ a; // $ a = 12; echo $ B; // $ B = 22; $ B = $ a ----- $; // $ a first incrementing by 1, then incrementing by 1, and then assigned to $ becho $ a; // $ a = 10 echo $ B; // $ B = 2

If the above logic is used

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

Can anyone tell us the detailed execution process of this code?

Khan, three sections of code are the same, so it is necessary to put so much? No matter$a++Or++$aYou only need to understand that the code is read from left to right.

$a++It means$aAnd then add 1 to it, so$b = $a++;The output result is$a=11;$b=10;

++$aIs first$aPerform auto-increment 1, and then$aValue, so$b = ++$a;The output result is$a = 11;$b=11;

If you can understand the above two paragraphs, it is not a problem to understand your section based on this idea. Let me talk about your last one:

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

This can be split

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

According to the above explanation,$a--The returned result is$aValue, and then$aAuto-minus 1.$a=9;$c=10;.--$aHere is$aSubtract 1 and then return$aValue, so at this time$a=8;$d=8;. The final result is naturally$a=8;$b=18;.

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.