The difference between ++i and i++ in php
- 2020-05-19 04:19:58
- OfStack
1. The usage of ++i (take a=++i, i=2 as an example)
Add 1 to i (i=i+1), then assign a (a=i),
So a is equal to 3, and i is equal to 3.
So a=++i is the same thing as i=i+1, a=i
2. The usage of i++ (take a=i++, i=2 as an example)
Assign the i value to the variable a (a=i), then add the i value to 1 (i=i+1),
So a is equal to 2, and i is equal to 3.
So a=i++ is the same thing as a=i, i=i+1
3. ++i and i++
a=++i = i++, a=i
a=i++ is equivalent to a=i, i++
4. When used alone, ++i and i++ are equivalent to i=i+1
If a new variable is assigned, ++i first adds 1 to the i value, while i++ first assigns i to the new variable.
Add 1 to i (i=i+1), then assign a (a=i),
So a is equal to 3, and i is equal to 3.
So a=++i is the same thing as i=i+1, a=i
2. The usage of i++ (take a=i++, i=2 as an example)
Assign the i value to the variable a (a=i), then add the i value to 1 (i=i+1),
So a is equal to 2, and i is equal to 3.
So a=i++ is the same thing as a=i, i=i+1
3. ++i and i++
a=++i = i++, a=i
a=i++ is equivalent to a=i, i++
4. When used alone, ++i and i++ are equivalent to i=i+1
If a new variable is assigned, ++i first adds 1 to the i value, while i++ first assigns i to the new variable.