Php analysis on $ I ++ and ++ $ I

Source: Internet
Author: User
This article uses the VLD tool to analyze the php opcode to explain the reason, first Map & amp; lt ;? Php $ i1; $ I + $ I ++; numberofops: 5 compiledvars :! 0 $ iline # * opfetchextreturnoperands -------... this article uses the VLD tool to analyze the php opcode, to explain the cause, first map

T1.php code

$ I = 1;

$ I + $ I ++;

Vld code

Number of ops: 5

Compiled vars :! 0 = $ I

Line # * op fetch ext return operands

---------------------------------------------------------------------------------

2 0> ASSIGN! 0, 1

3 1 POST_INC ~ 1! 0

2 ADD ~ 2! 0 ,~ 1

3 FREE ~ 2

4 4> RETURN 1

Branch: #0; line: 2-4; sop: 0; eop: 4

Path #1: 0,

T2.php code

$ I = 1;

$ I + $ I ++

Vld2 code

Number of ops: 6

Compiled vars :! 0 = $ I

Line # * op fetch ext return operands

---------------------------------------------------------------------------------

2 0> ASSIGN! 0, 1

3 1 ADD ~ 1! 0 ,! 0

2 POST_INC ~ 2! 0

3 ADD ~ 3 ~ 1 ,~ 2

4 FREE ~ 3

4 5> RETURN 1

Branch: #0; line: 2-4; sop: 0; eop: 5

Path #1: 0,

We can compare the two pictures of 2, 4 to see why the results surprised you.

First, analyze the execution results of 1 and 2.

First, assign $ I to 1, $ I ++ auto-increment, and copy result 1 to the temporary variable ~ 1 ($ I), then $ I is automatically increased to 2, that is! 0 = 2. of course, the final result ~ 2 = ~ 1 +! 0 = 3;

Analyze 3, 4

First, assign $ I to 1, and then $ I + $ I =! 0 +! 0 = ~ 1 = 2, and then $ I ++ auto-increment, copy result 1 to the temporary variable ~ 2 ($ I), the final result ~ 1 + ~ 2 = 2 + 1 = 3;

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.