I ++ problems

Source: Internet
Author: User
I ++ int I = 3;
I = I + I ++ I;
The above code outputs 11 in both Java/C #/JavaScript, but 12 in PHP. what is the difference between PHP computing and three for him? Think Twice. please give me some advice!


Reply to discussion (solution)

According to PHP

3 + 4 + 5 ~

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

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

According to PHP

3 + 4 + 5 ~


No. I tried it.
$ I = 3;
$ J = 3;
$ K = 3;
$ I = $ I + $ I ++ $ I;
$ J = $ j ++ $ j + $ j;
$ K = ++ $ k + $ k ++ $ k;
Echo $ I;
Echo $ j;
Echo $ k;
Output 12 13 13

$i = 3;$a = $i + $i++ + ++$i;echo $a; //12
This is a php BUG.
It seems that all php5 has this problem (at least three versions at hand)
The problem lies in the first $ I value transfer method, such as writing
$i = 3;$a = ($d=$i) + $i++ + ++$i;echo $a; //11
That's correct.
Obviously, the first $ I is passed as a reference, because the following $ I ++ is actually involved in the operation of 4 rather than 3

Remove ++ $ I to make it clearer.

In this case, unnecessary errors may occur due to version issues. Alas...

In this case, unnecessary errors may occur due to version issues. Alas... no. since all versions of php5 are like this, it doesn't matter. Besides, such code has no practical significance.
If you really need it, just write $ I + $ I ++ 2 * $ I ++.

Reference the reply from happypiggy2010 on the fifth floor: In this case, unnecessary errors may occur due to version problems. Alas... no. since all versions of php5 are like this, it doesn't matter. Besides, such code has no practical significance.
If you really need it, you just need to figure out $ I + $ I ++ writing 2 * $ I ++. in the PHP computing expression, if a variable appears multiple times, its value is determined by the second variable involved in calculation.

$i = 3;$a = $i + ($i + ($i+$i++));echo $i,'#',$a;//4#15,a=4+(4+(4+3))

$i = 3;$a = $i + $i + ($i+$i++);echo $i,'#',$a;//4#14,a=3+3+(4+4))

Reference the reply from happypiggy2010 on the fifth floor: In this case, unnecessary errors may occur due to version problems. Alas... no. since all versions of php5 are like this, it doesn't matter. Besides, such code has no practical significance.
If you really need it, just write $ I + $ I ++ 2 * $ I ++.

$i = 3;$a = $i + $i++ + ($i+ ++$i);echo $i,'#',$a;//5#17,a=4+3+(5+5))

Too advanced, Daniel!

Too advanced, Daniel!

Let me stir up, hey

$i=3;$a = ++$i - ($i + $i++); echo $a, ',', $i;//-5,5$i=3;$a = $i++ - (++$i + $i); echo $a, ',', $i;//-7,5$i=3;$a = ++$i - ($i + ++$i);echo $a, ',', $i; // -6,5

According to PHP

3 + 4 + 5 ~ 3 + 3 + 5 in Java and 4 + 3 + 5 in PHP

Reference the reply from baoying34 on the first floor: According to PHP

3 + 4 + 5 ~


No. I tried it.
$ I = 3;
$ J = 3;
$ K = 3;
$ I = $ I + $ I ++ $ I;
$ J = $ j ++ $ j + $ j;
$ K = ++ $ k + $ k ++ $ k;
Echo $ I;
Echo $ j ;...... 3 + 3 + 5 in Java and 4 + 3 + 5 in PHP

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.