Differences and efficiency between I ++ and I in PHP _ php instance

Source: Internet
Author: User
This article mainly introduces the differences and efficiency of I ++ and ++ in PHP. it is very helpful for reference. if you need it, you can refer to it. Let's take a look at the basic differences:

I ++: first use the current value of I in the expression where I is located, and then add 1 to the expression

++ I: Let I add 1 first, and then use the new value of I in the expression where I is located.

When I read some video tutorials and wrote for loops, I wrote ++ I instead of I ++. I searched the internet and found the efficiency problem.

++ I is equivalent to the following code:

i += 1; return i; 

I ++ is equivalent to the following code:

j = i; i += 1; return j;

Of course, if the compiler will optimize these differences, the efficiency will be almost the same.

Let's explain in detail the differences between ++ I and I ++.

1. usage of ++ I (using a = ++ I, I = 2 as an example)

Add the I value to 1 (that is, I = I + 1) and then assign it to variable a (that is, a = I ),

The final a value is 3, and the I value is 3.

So a = ++ I is equivalent to I = I + 1, a = I

2. I ++ usage (take a = I ++, I = 2 as an example)

First, assign the I value to variable a (that is, a = I), and then add 1 (that is, I = I + 1) to the I value ),

Then, the final a value is 2, and the I value is 3.

So a = I ++ is equivalent to a = I, I = I + 1

3. ++ I and I ++

A = ++ I is equivalent to I ++, a = I

A = I ++ is equivalent to a = I, I ++

4. when ++ I and I ++ are used independently, they are equivalent to I = I + 1.

If a new variable is assigned, ++ I first adds 1 to the I value, and I ++ first assigns I to the new variable.

The above section describes the differences and efficiency between I ++ and I in PHP. I hope to help you, if you have any questions, please leave a message and the editor will reply to you in time. I would like to thank you for your support for the script home website!

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.