PHP Strange Parsing method

Source: Internet
Author: User
Code One:
$i = 1;
$i = $i + + + + $i;
echo $i;

Code two:
$i = 1;
$i = + + $i + $i + +;
echo $i;

Both of these codes result in 4.

I don't think I can understand the parsing principle of PHP to ask Big Brother pointing


Reply to discussion (solution)

What's wrong with it?
$i = 1;
$i = $i + + + + $i;
==>
$i = 1 (but because of $i++, so $i is 2) + 3 (+ + $i $i is 2, plus 1 is 3)

$i = 1;
$i = + + $i + $i + +;
==>
$i = 2 + 2

What's wrong with it?
$i = 1;
$i = $i + + + + $i;
==>
$i = 1 (but because of $i++, so $i is 2) + 3 (+ + $i $i is 2, plus 1 is 3)

$i = 1;
$i = + + $i + $i + +;
==>
$i = 2 + 2

Do the + + $i first
And the addition should be a.
Last $i + +
It's supposed to be 5.


$i = 1;
$i = $i + +;
echo $i;
echo $i;

Output two X 1

+ + operation was swallowed.

The reason for the seemingly returning 4 is that the last $i + + is swallowed up at $i = ...

The reason for the seemingly returning 4 is that the last $i + + is swallowed up at $i = ...
Your understanding is not the same as mine.
That's how I analyze it.
$i = $i + +
First you have to admit that this is an assignment operation that assigns the right value to the left (that is, $i)

First look to the right, the current value of I is 1, and the value of i++ itself is 1, but the value of I after i++ operation is 2.
The point is finally, to replicate. It says that i++ itself is 1, copy 1 to I, and I will change from 2 to 1.
Did not swallow a say.

Right! You understand the right thing!

$i = 1; $ii = + + $i + $i ++;echo "\ $i = $i \ $ii = $ii";//$i =3 $ii =4$i = 1; $ii = $i ++;echo ' $i = '. $i; $i =2echo ' $ii = '. $ii; $ii =1


The reason for the seemingly returning 4 is that the last $i + + is swallowed up at $i = ...
Your understanding is not the same as mine.
That's how I analyze it.
$i = $i + +
First you have to admit that this is an assignment operation that assigns the right value to the left (that is, $i)

First look to the right, the current value of I is 1, and the value of i++ itself is 1, but the value of I after i++ operation is 2.
The point is finally, to replicate. It says that i++ itself is 1, copy 1 to I, and I will change from 2 to 1.
Did not swallow a say.


First, assign the value after + +

$i = $i + +;

Seems to have been interpreted like this.
$i = Funcation () {return $i + +;}

The + + operation is treated as a local variable

The foundation did not play well. Study the difference between pre-plus and post-plus.

In this case, the + + as a function, ++a return is a+1, and a++ return is a. However, after two operations, A is a+1, knowing that the return value is not different.

In this case, the + + as a function, ++a return is a+1, and a++ return is a. However, after two operations, A is a+1, knowing that the return value is not different.

Both in $i + +

The result should be $i =2

$j = $i + +;
$i = $i + +;

Not the same for the result $i

$i = $i + + This is undefined behavior (Undefined behavior) See Http://stackoverflow.com/questions/1998903/php-i-i-crashed-the-server

The reason for the seemingly returning 4 is that the last $i + + is swallowed up at $i = ...
No, you have to understand that one is performed after the operation, one is the operation after the execution, you understand the wrong $i++ and + + $i Difference

$i + + is the first to take plus, + + $i, this is the first plus the value
$i = $i + + + + $i;
This is supposed to be
$i = 1 (first fetch and then add, so here is still 1) + 3 (because before $i++ got 2, here first add again, so it is 3);

The first one is the
The second one is 1+3.
So it's all equal to 4.
$i + + This expression is to be $i +1, and this expression value is also $i+1,
+ + $i This expression will also $i +1, but the value of this expression is $i, not $i+1, understand?

This is a two different expression.


The first one is the
The second one is 1+3.
So it's all equal to 4.
$i + + This expression is to be $i +1, and this expression value is also $i+1,
+ + $i This expression will also $i +1, but the value of this expression is $i, not $i+1, understand?

This is a two different expression.

Dizzy, said the reverse ...

The first one is 1+3.
And the second one is the
So it's all equal to 4.
+ + $i This expression is to be $i +1, and this expression value is also $i+1,
$i + + This expression will also $i +1, but the value of this expression is $i, not $i+1, understand?

This is a two different expression.

Did not learn how long, personally think it should be so understood, $i + + + + + First 1 to add + + $i (2) finally equals 3, because the front of the $i++ has not been self-increasing, the entire expression after the run, $i there is a self-increment, so is 4

Maybe that's what you got.
$i = 1;
$j = $i + + + + $i;
echo $i. ': ' $j;

$i = 1;
$j = + + $i + $i + +;
echo $i. ': ' $j;

Seems to be because

$i = $i + +;

Be resolved to

$i = $i; $i + 1;

Instead of

$i = $i; $i = $i +1;


Maybe both of them have a 1+3.

  • 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.