Will you add? I am struggling! Help

Source: Internet
Author: User
Will you add? I am struggling! Help
$a = 1;$c = $a + ($a++);var_dump($c);//3   $a = 1;$c = (++$a)+$a;var_dump($c);//4   $a = 1;$c = (++$a)+($a++);var_dump($c);//4   $a = 1;$b = &$a;$c = (++$a)+($a++);var_dump($c);//5

Please explain.


Reply to discussion (solution)


1) the expression is executed from left to right, so $ a is still 1, 1 + 2 = 3.

2) It is also executed from left to right, ++ $ a is 2, then $ a has been self-added, so 2 + 2 = 4

3) ++ $ a is the first auto-increment. then, the value of $ a is 2, and the value of $ a ++ is the second auto-increment. Therefore, 2 + 2 = 4

4) because the reference value is changed, the value is 2 + 3 = 5.

$ A = 1;
$ C = $ a + ($ a ++ );
Var_dump ($ c, $ a); // 3, 2
$ A ++ is equivalent to $ a = $ a + 1
So
$ C = $ a + ($ a ++ );
Equivalent
$ C = 2 + (1 );
Based on addition switching law
$ C = $ a + ($ a ++ );
Equivalent
$ C = ($ a ++) + $ a; // It's not hard to understand it?
Equivalent
$ C = (1) + 2;

Other similar

1) the expression is executed from left to right, so $ a is still 1, 1 + 2 = 3.

2) It is also executed from left to right, ++ $ a is 2, then $ a has been self-added, so 2 + 2 = 4

3) ++ $ a is the first auto-increment. then, the value of $ a is 2, and the value of $ a ++ is the second auto-increment. Therefore, 2 + 2 = 4

4) because the reference value is changed, the value is 2 + 3 = 5.

I want to explain the difference between the third and the first. since it is post-auto-addition, why is the first, not the second?

For
$ A = 1;
$ C = (++ $ a) + ($ a ++ );
Var_dump ($ c, $ a); // 4, 3

So I understand it.
(++ $ A) add 1 first and then value
Get the value of $ a (2) for calculation
($ A ++) first value, followed by 1
Get the value of $ a (2) for calculation
$ C = (2) + (2)
Although $ a is already 3 at this time
But all involved in calculation are scalar, with no variable
Note the difference with Formula 1

I still don't quite understand the difference between Xueda and the 1 type. if we analyze the 3 type, the result is 2, of course, this is definitely not true. where are the core areas of the 1 and 3 types ?....

$ C = $ a + ($ a ++ );
Involved in calculation using variables

I still don't quite understand the difference between Xueda and the 1 type. if we analyze the 3 type, the result is 2, of course, this is definitely not true. where are the core areas of the 1 and 3 types ?....

++ $
Indicates that $ a is first added with 1, and then $ a is returned.
Equivalent
$ A = $ a + 1;
Returns $;

$ A ++ indicates that $ a is returned first and then $ Aplus 1 is given.

Returns $;
$ A = $ a + 1;


Eg:
$ A = 1;

Echo $ a ++; // returns $ a first, and then adds 1 to $ a, which is equivalent to echo $ a; $ a = $ a + 1; the output is 1.

Echo ++ $ a; // add 1 to $ a first, and then return $ a equivalent to $ a = $ a + 1; echo $ a; output is 2

I still don't quite understand the difference between Xueda and the 1 type. if we analyze the 3 type, the result is 2, of course, this is definitely not true. where are the core areas of the 1 and 3 types ?.... You are also dizzy, right?

For
$ A = 1;
$ C = (++ $ a) + ($ a ++ );
Var_dump ($ c, $ a); // 4, 3

So I understand it.
(++ $ A) add 1 first and then value
Get the value of $ a (2) for calculation
($ A ++) first value, followed by 1
Get the value of $ a (2) for calculation
$ C = (2) + (2)
Although $ a is already 3 at this time
But all involved in calculation are scalar, with no variable
Note the difference with Formula 1
According to the two answers given by bamboo, the first explanation is that when there are parentheses, we first calculate the values in the brackets and then convert them into scalar values before calculation.
I still have doubts. what is the final result of 5?

1) the expression is executed from left to right, so $ a is still 1, 1 + 2 = 3.

2) It is also executed from left to right, ++ $ a is 2, then $ a has been self-added, so 2 + 2 = 4

3) ++ $ a is the first auto-increment. then, the value of $ a is 2, and the value of $ a ++ is the second auto-increment. Therefore, 2 + 2 = 4

4) because the reference value is changed, the value is 2 + 3 = 5.
The first one seems to be wrong.

It does not matter whether there are many parentheses. you are all addition operations. The combination law of addition shows that no matter whether there are parentheses or where parentheses are added, the final result will not be affected.

$a = 1;$c = (++$a)+($a++);var_dump($c);//4$a = 1;$c = ++$a + $a++;var_dump($c);//4$a = 1;$c = $a++ + ++$a;var_dump($c);//4

It does not matter whether there are many parentheses. you are all addition operations. The combination law of addition shows that no matter whether there are parentheses or where parentheses are added, the final result will not be affected.

$a = 1;$c = (++$a)+($a++);var_dump($c);//4$a = 1;$c = ++$a + $a++;var_dump($c);//4$a = 1;$c = $a++ + ++$a;var_dump($c);//4
Then, how can I explain the one that becomes 5 after the last reference.

For
$ A = 1;
$ B = & $;
Var_dump ($ c); // 5
Due to the existence of applications, the situation must be complex.

Because there is a process of assigning values to $ B, this is equivalent
$ A = 1;
$ C = (++ $ a) + ($ B = $ a + 1 );
Var_dump ($ c); // 5

For
$ A = 1;
$ B = & $;
Var_dump ($ c); // 5
Due to the existence of applications, the situation must be complex.

Because there is a process of assigning values to $ B, this is equivalent
$ A = 1;
$ C = (++ $ a) + ($ B = $ a + 1 );
Var_dump ($ c); // 5 does not understand. Still a little dizzy.

In the execution process, $ a ++ does not affect the value of $ a, but changes after the execution is completed, similar to for $ I =... loop, $ I is worth more than 1 after exiting the loop
++ $ A changes the value of $ a during execution, and remains unchanged after execution.

$ B = & $ a causes $ a ++ to perform the "after execution" action, so $ a ++ also increases progressively.

I used to understand it like this, and I don't know if it is appropriate.

$ C = $ a + ($ a ++ );
Involved in calculation using variables


I still don't quite understand the difference between Xueda and the 1 type. if we analyze the 3 type, the result is 2, of course, this is definitely not true. where are the core areas of the 1 and 3 types ?....

$ A = 1;
$ C = $ a + ($ a ++ );
The red $ a is used in the calculation of variables, but why is it 2? Is it true that $ a + ($ a ++) runs ($ a ++) first?
The result of dropping the brackets of $ a ++ in the past is the same, so it has nothing to do with the brackets. Isn't the expression calculated from left to right?

$ C = $ a + ($ a ++ );
Involved in calculation using variables


I still don't quite understand the difference between Xueda and the 1 type. if we analyze the 3 type, the result is 2, of course, this is definitely not true. where are the core areas of the 1 and 3 types ?....

It is estimated that this understanding

Function returnFirst (& $)
{
$ Return = $;
$ A = $ a + 1;
Return $ return;
}

Function plusFirst (& $)
{
$ A = $ a + 1;
$ Return = $;
Return $ return;
}


$ A = 1;
$ C = $ a + returnFirst ($ a); // $ c = $ a + (a ++)
Var_dump ($ c, $ a); // 3, 2


For
$ A = 1;
$ B = & $;
Var_dump ($ c); // 5
Due to the existence of applications, the situation must be complex.

Because there is a process of assigning values to $ B, this is equivalent
$ A = 1;
$ C = (++ $ a) + ($ B = $ a + 1 );
Var_dump ($ c); // 5




If yes
$ C = ($ a ++) + ($ a ++) to understand as $ c = ($ B = $ a + 1) + ($ B = $ a + 1?
$ C = ($ a ++) + ($ a ++) returns 3


For
$ A = 1;
$ B = & $;
Var_dump ($ c); // 5
Due to the existence of applications, the situation must be complex.

Because there is a process of assigning values to $ B, this is equivalent
$ A = 1;
$ C = (++ $ a) + ($ B = $ a + 1 );
Var_dump ($ c); // 5




If yes
$ C = ($ a ++) + ($ a ++) to understand as $ c = ($ B = $ a + 1) + ($ B = $ a + 1?
The returned result of $ c = ($ a ++) + ($ a ++) is 3. after listening to your suggestion, I think the explanation of bamboo is wrong...

For
$ A = 1;
$ B = & $;
Var_dump ($ c); // 5
Due to the existence of applications, the situation must be complex.

Because there is a process of assigning values to $ B, this is equivalent
$ A = 1;
$ C = (++ $ a) + ($ B = $ a + 1 );
Var_dump ($ c); // 5

Nnd is a tough issue.

When $ B = & $ a, it seems that only (++ $ a) on the far left of the operation expression will cause an unexpected situation.
That is:
(++ $ A) + expression 2 + expression 3 ......
Note: The expression is ($ a ++) or (++ $)

All results starting from the third expression are regularly searchable, that is, only the results of (++ $ a) + expression 2 are affected.

Assume that the sum of the first n expressions is Sn. $ a = 5 after calculation;
Then Sn + ($ a ++) = Sn + 5;
Sn + (++ $ a) = Sn + 6

That is to say, the response considers ($ a ++) as function returnFirst (& $ a) {$ return = $ a; $ a = $ a + 1; return $ return ;} calculate (++ $ a) as function plusFirst (& $ a) {$ a = $ a + 1; return $ a;} correctly.


Is it a design defect of php design on this issue. If so, it would be very high. I encountered this level of problem first in my life.


For
$ A = 1;
$ B = & $;
Var_dump ($ c); // 5
Due to the existence of applications, the situation must be complex.

Because there is a process of assigning values to $ B, this is equivalent
$ A = 1;
$ C = (++ $ a) + ($ B = $ a + 1 );
Var_dump ($ c); // 5

Nnd is a tough issue.

When $ B = & $ a, it seems that only (++ $ a) on the far left of the operation expression will cause an unexpected situation.
That is:
(++ $ A) + expression 2 + expression 3 ......
Note: The expression is ($ a ++) or (++ $)

All results starting from the third expression are regularly searchable, that is, only the results of (++ $ a) + expression 2 are affected.

Assume that the sum of the first n expressions is Sn. $ a = 5 after calculation;
Then Sn + ($ a ++) = Sn + 5;
Sn + (++ $ a) = Sn + 6

That is to say, the response considers ($ a ++) as function returnFirst (& $ a) {$ return = $ a; $ a = $ a + 1; return $ return ;} calculate (++ $ a) as function plusFirst (& $ a) {$ a = $ a + 1; return $ a;} correctly.


Is it a design defect of php design on this issue. If so, it would be very high. I encountered this level of problem first in my life. Are you confused?


For
$ A = 1;
$ B = & $;
Var_dump ($ c); // 5
Due to the existence of applications, the situation must be complex.

Because there is a process of assigning values to $ B, this is equivalent
$ A = 1;
$ C = (++ $ a) + ($ B = $ a + 1 );
Var_dump ($ c); // 5

Nnd is a tough issue.

When $ B = & $ a, it seems that only (++ $ a) on the far left of the operation expression will cause an unexpected situation.
That is:
(++ $ A) + expression 2 + expression 3 ......
Note: The expression is ($ a ++) or (++ $)

All results starting from the third expression are regularly searchable, that is, only the results of (++ $ a) + expression 2 are affected.

Assume that the sum of the first n expressions is Sn. $ a = 5 after calculation;
Then Sn + ($ a ++) = Sn + 5;
Sn + (++ $ a) = Sn + 6

That is to say, the response considers ($ a ++) as function returnFirst (& $ a) {$ return = $ a; $ a = $ a + 1; return $ return ;} calculate (++ $ a) as function plusFirst (& $ a) {$ a = $ a + 1; return $ a;} correctly.


Is it a design defect of php design on this issue. If so, it would be very high. I encountered this level of problem first in my life.

That is to say, the response considers ($ a ++) as function returnFirst (& $ a) {$ return = $ a; $ a = $ a + 1; return $ return ;} calculate (++ $ a) as function plusFirst (& $ a) {$ a = $ a + 1; return $ a;} correctly.
This sentence is not said .. You returned the value of $ a after half a day,



For
$ A = 1;
$ B = & $;
Var_dump ($ c); // 5
Due to the existence of applications, the situation must be complex.

Because there is a process of assigning values to $ B, this is equivalent
$ A = 1;
$ C = (++ $ a) + ($ B = $ a + 1 );
Var_dump ($ c); // 5

Nnd is a tough issue.

When $ B = & $ a, it seems that only (++ $ a) on the far left of the operation expression will cause an unexpected situation.
That is:
(++ $ A) + expression 2 + expression 3 ......
Note: The expression is ($ a ++) or (++ $)

All results starting from the third expression are regularly searchable, that is, only the results of (++ $ a) + expression 2 are affected.

Assume that the sum of the first n expressions is Sn. $ a = 5 after calculation;
Then Sn + ($ a ++) = Sn + 5;
Sn + (++ $ a) = Sn + 6

That is to say, the response considers ($ a ++) as function returnFirst (& $ a) {$ return = $ a; $ a = $ a + 1; return $ return ;} calculate (++ $ a) as function plusFirst (& $ a) {$ a = $ a + 1; return $ a;} correctly.


Is it a design defect of php design on this issue. If so, it would be very high. I encountered this level of problem first in my life.

That is to say, the response considers ($ a ++) as function returnFirst (& $ a) {$ return = $ a; $ a = $ a + 1; return $ return ;} calculate (++ $ a) as function plusFirst (& $ a) {$ a = $ a + 1; return $ a;} correctly.
This sentence is not said .. You returned the value of $ a after half a day,

I mean
$ A = 1;
$ C = $ a + ($ a ++ );
Var_dump ($ c); // 3

$ A = 1;
$ C = (++ $ a) + $;
Var_dump ($ c); // 4

$ A = 1;
$ C = (++ $ a) + ($ a ++ );
Var_dump ($ c); // 4

The second and third cases are easy to understand, that is, they are simply from left to right and conform to the understanding of "first added and then returned" or "first returned and then added ".
In the first case, if it is an operation from the left to the right, it does not seem to conform to the understanding of "first return and then add. However, if the order is calculated from the right to the left, it is explained.
I guess when there is a variable or an auto-increment operation in the formula, php will first perform the auto-increment operation in priority.
Just like
($ A ++) is equivalent to function returnFirst (& $ a) {$ return = $ a; $ a = $ a + 1; return $ return ;}
(++ $ A) is equivalent to function plusFirst (& $ a) {$ a = $ a + 1; return $ ;}

Therefore, if you replace ($ a ++) with returnFirst (& $ a), replace (++ $ a) with plusFirst (& $)
The above three cases can be explained.

However, you listed the fourth case where there is a variable reference.
$ A = 1;
$ B = & $;
$ C = (++ $ a) + ($ a ++ );
Var_dump ($ c); // 5

Note:
($ A ++) or (++ $ a) a subexpression.
Several ($ a ++) or (++ $ a) values are added, which is called an expression.

When a variable is referenced, when you add multiple ($ a ++) and (++ $)
Only when (++ $ a) is on the leftmost side of an expression will an unexpected situation occur (indicating that the result is inconsistent with that replaced by a function ).
Only the results of (++ $ a) + "subexpression 2" are affected.
That is to say, if you already know the result of (++ $ a) + "subexpression 2, you can use a function instead of the "subexpression" after "subexpression 2" to calculate the result.


To sum up, I think ($ a ++) can be understood using returnFirst ($ a), and (++ $ a) can be understood using plusFirst ($.
The fourth unexpected situation may be the php design defect that was not taken into account when auto-increment and reference occur. Of course, this is just my guess.

C/C ++ and php do not have the same results after adding. php uses the zend kernel to perform variable allocation in the memory, c/c ++ has a better understanding than c/c ++.

C/C ++ and php do not have the same results after adding. php uses the zend kernel to perform variable allocation in the memory, c/c ++ has a better understanding than c/c ++. Could you please help us renew it... Well understood.

After reading various explanations on the floor, I had a lot of questions in my mind. I always felt that every explanation was far-fetched. I think it should be regarded as a bug of php, rather than a feature.
Use the vld tool to view the php operation process.
The expression value obtained in Step + + $ a is represented by $1.
The expression value obtained in step 2 $ a ++ is represented by $2.
Step 3: calculate $1 + $2.
This process should have no objection.

What is the result of ++ $? What is the result of $ a ++?
For ++ $ a, php uses the incremental $ a to represent the expression result, that is, $1 and $ a point to the same zval at the bottom layer of php.
For $ a ++, php first copies $ a as $2, and then adds $ a to 1, that is, since then, $2 and $ a will not have a half-cent relationship.

The key to the problem is that $ a ++ in step 2 changes the value of $ a, and because $ a is of the reference type, this operation will not be performed on a copy of $, this will modify the expression value $1 in the first step synchronously (because $ a and $1 are the same thing ).

If you are interested, you can look at the php source code and search for PRE_INC in zend_vm_def.h to find the underlying implementation of the front ++. PRE_DEC, that is, the frontend-operation also has the same problem.
With this understanding, you can easily construct more expressions that do not meet the expected results.

You can give it a try.
$ A = 2;
$ B = & $;

Echo ++ $ a + $ a ++;
Echo ++ $ a + $ a * = 2;

Echo -- $ a + $ --;
Echo -- $ a-$ --;

Echo ++ $ 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.