Why do these both post increment in PHP give the same answer?

I am trying to run the following code in PHP via localhost, but this gives an unexpected output!

<?php $a = 1; echo ($a+$a++); // 3 ?> 

// answer is 3, but the answer should be 2 due to the increment of posts, here is another code, and it gives the same answer! why?

 <?php $a = 1; echo ($a+$a+$a++); ?> 

// the answer is 3 more !!!

+8
php post-increment
source share
3 answers

The PHP manual says the following:

Operator precedence and associativity only determine how expressions are grouped; they do not determine the order of evaluation. PHP (in the general case) does not indicate in which order the expression is evaluated, and code that involves a certain evaluation order should be avoided, as the behavior may vary between versions of PHP or depending on the surrounding code.

So, what does this apply to, PHP does not explicitly determine what the end result of these types of operators is, and it may even change between versions of PHP. We call this behavior undefined, and you should not rely on it.

You may find the exact reason somewhere in the source why this order is chosen, but there can be no logic.

Your two examples are evaluated as follows:

 <?php $a = 1; echo ($a + $a++); // 3 ?> 

Really becomes:

 <?php $a = 1; $b = $a++; echo ($a + $b); // a = 2, b = 1 ?> 

Second example:

 <?php $a = 1; echo ($a + $a + $a++); // 3 ?> 

becomes:

 <?php $a = 1; $b = $a + $a; $a++; echo $b + $a; // 3 ?> 

I hope this makes sense. You are right that there is no rigid logic behind this.

+1
source share

BEHAVIOR OF USE OF INTERACTION IN A SECRET LINE WITH A CALCULATION IS NOT DEFINED!

The compiler may generate other code that you expect.


A simple answer from my teacher:

NEVER USE A RECORD / DECREE OPERATOR IN ONE CALCULATED LINE!

This behavior is undefined - computers calculate in a different order than people do.

OK:

 $d = $i++; $i++; for ($i = 0; $i < 5; $i++) 

MAY BE A PROBLEM (you may not read it correctly):

 $d = $array[$i++]; 

There will be a problem:

 $d = $i++ + 5 - --$k; 

EDIT:

I wrote the same code in C ++. Check the assembler code and the result, as I said: Mathematics with growth is not logic for people, but you cannot say that someone implemented it incorrectly.

As someone posted in a comment:

 line #* EIO op fetch ext return operands ------------------------------------------------------------------------------------- 2 0 E > ASSIGN !0, 1 3 1 POST_INC ~2 !0 2 ADD ~3 !0, ~2 3 ECHO ~3 16 4 > RETURN 1 //$a = 1; //echo ($a+$a++); line #* EIO op fetch ext return operands ------------------------------------------------------------------------------------- 2 0 E > ASSIGN !0, 1 3 1 ADD ~2 !0, !0 2 POST_INC ~3 !0 3 ADD ~4 ~2, ~3 4 ECHO ~4 5 > RETURN 1 //$a = 1; //echo ($a+$a+$a++); 

Translation into human language:

 $a = 1; echo ($a + $a++); // After changing to 'computer logic' (ASM): $c = 1; // for increment operation $b = $a; // keep value 'before' add +1 operation $a += $c; // add +1 operation $d = $a + $b; // calculate value for 'echo' echo $d; // print result 
-one
source share

Because the ++ sign is an incremental variable operator. So your

$a = 1

$a++ = 2

($a+$a++) = (1+2) = 3

That is why it shows 3 as the answer.

-3
source share

All Articles