Php's $ I ++, ++ $ I Questions

Source: Internet
Author: User
The following two pieces of code under {code...} are confusing: {code...} There is no difference between I ++ and I, so I don't understand it? {Code...} Why are the output values 3?

  '; Echo $ I; break; case 3: echo' output 3
'. $ I; break; case 1: echo "output 1
"; Break; default: break ;}

The following two pieces of code are confusing:


  ';}for($i=0;$i<3;$i++){    $j=$i;    echo $j.'-'.$i.'
';}

I ++ and I are no different here. Don't you understand?


  ';$a=1;echo $a+$a+$a++;

Why are all output values 3?

Reply content:

  '; Echo $ I; break; case 3: echo' output 3
'. $ I; break; case 1: echo "output 1
"; Break; default: break ;}

The following two pieces of code are confusing:


  ';}for($i=0;$i<3;$i++){    $j=$i;    echo $j.'-'.$i.'
';}

I ++ and I are no different here. Don't you understand?


  ';$a=1;echo $a+$a+$a++;

Why are all output values 3?

$ A = 1;
Echo $ a + $ a ++ .'
';
$ A = 1;
Echo $ a + $ a ++;

The execution sequence of the first echo is as follows:

$ A = 1;
$ A ++ returns 1 and saves it to the Temporary Variable ~ 1
This time! 0 (that is, $ a has changed from auto-increment to 2)
~ 1 +! 0 (1 + 2) Save to temporary variable ~ 2
Output 3

The execution sequence of the second echo is as follows:


The execution sequence is
$ A = 1;
Save result 2 of $ a + $ a to the Temporary Variable ~ 1
$ A ++ returns 1 and saves it to the Temporary Variable ~ 2
~ 1 + ~ 2 (1 + 2) Save to the Temporary Variable ~ 3
Output 3

For a for statement, the write results are the same. The actual execution of I ++ and I is different:
For example:

$ N = 1; echo $ n ++; // 1 first use echo $ n; // 2 and then execute + + echo ''; $ n = 5; echo ++ $ n; // 6 execute ++ first, and then use the value of $ n echo $ n; // 6

So

$a=1;echo $a+$a++.'
'; // 2 + 1$a=1;echo $a+$a+$a++; // (1+1)+1

In short, the operator has the same priority, and its combination direction determines the order of value.
In general, ++ has a high priority, so $ a + $ a ++ uses the latter $ a value first, and because it is the same variable, it is combined from left to right, the first $ a value is changed to 2, so the result 2 + 1 gets 3.
However, whether it is I ++ or ++ I (only the local combination sequence can be changed), it is an addition operation (with the same priority). Obviously, the addition operation is from left to right, ($ a + $ a) + $ a ++. The latter $ a ++ does not affect the value of the first two $ a values of the latter expression.

Because the first piece of code outputs $ I.

The second code outputs $ a + ($ a ++)

It is actually the difference between $ a ++ and ++ $. It is only necessary to output them, otherwise there is no difference.

For example:
$ A = 1, $ B = 1;
$ A ++;
+ + $ B;
Echo 'this is the result after $ a ++: '. $ a; // 2
Echo 'this is the result after ++ $ B: '. $ B; // 2

The output is the same as the result after ++ (auto-increment.

If it is directly output, it will be different:

$ A = 1, $ B = 1;
Echo 'This is $ a ++:'. $ a ++; // 1
Echo 'this is ++ $ B: '. ++ $ B; // 2

Because it is directly output, it will be different. The reason is:
$ A ++ returns the value of $ a first and then auto-increment.

++ $ B directly returns the auto-increment value.

See http://php.net/manual/zh/control-structures.for.php
Whether I ++ or ++ I, the operation is performed after the end of this loop, and then enters the next loop.

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.