Comparison between ++ I and I ++ in php _ PHP Tutorial

Source: Internet
Author: User
Comparison between ++ I and I ++ in php. In many programming languages, ++ I and I ++ add + 1 operations to variables, but there are successive problems. let's introduce the differences between them. 1. usage of ++ I (a ++ I and I ++ are used in many programming, and the addition of the Plus 1 operation on the variable is problematic successively, next I will introduce some differences between them.


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.

Performance Optimization

The code is as follows:
Method 1:

$ Begin = time ();
$ I = 0;
While (++ $ I <10000)
{
$ J = 0;
While (++ $ j <10000)
;
;
}
$ End = time ();

Time: 16 s

Method 2:

$ Begin = time ();
$ I = 0;
While ($ I <10000)
{
$ J = 0;
While ($ j <10000)
+ + $ J;
++ $ I;
}
$ End = time ();

Time: 13 s

Method 3:

$ Begin = time ();
$ I = 0;
While ($ I <10000)
{
$ J = 0;
While ($ j <10000)
$ J ++;
$ I ++;
}
$ End = time ();

Time: 15 s

Method 4:

$ Begin = time ();
$ I = 0;
While ($ I ++ <10000)
{
$ J = 0;
While ($ j ++ <10000)
;
;
}
$ End = time ();
Time: 13 s

Compare the first and second methods, because in PHP, the OPCODE is finally executed, and each line of opline

There are two operands. for an operand, there are generally three types of access methods: temporary variables, variables, and compile-time variables.

Among them, the fastest access is the third type. compiler variables, during OpCode execution, will add a level-1 reference of a variable stored in a hash

Structure, used to speed up access.

Http://www.bkjia.com/PHPjc/629024.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/629024.htmlTechArticle++ I and I ++ are in many programming, the addition of variable + 1 operation, but there are successive problems, below I will introduce them in the running of some of the differences. 1. usage of ++ I (using a = ++...

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.