Home » Php » why these both post increment in PHP gives the same answer?

why these both post increment in PHP gives the same answer?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am trying to run the following code in PHP through localhost, but its giving the unexpected output!

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

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

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

//answer is still 3 !!!

How to&Answers:

The PHP manual says the following:

Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation. PHP does not (in the general case) specify in which order an expression is evaluated and code that assumes a specific order of evaluation should be avoided, because the behavior can change between versions of PHP or depending on the surrounding code.

So what this comes down to, PHP doesn’t explicitly define what the end-result is of those types of statements, and it may even change between PHP versions. We call this undefined behavior, and you shouldn’t rely on it.

You might be able to find an exact reason somewhere in the source why this order is chosen, but there might not be any logic to it.

Your two examples are being evaluated as follows:

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

Really becomes:

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

Your 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 kind of makes sense. You’re right that there’s no hard logic behind this.

Answer:

BEHAVIOR OF INCREMENT OPERATION IN SAME LINE WITH CALCULATION IS NOT DEFINIED!

Compilator can generate different code then you expect.


Simple answer from my teacher:

NEVER USE INCREMENT/DECREMENT OPERATOR IN SAME LINE WITH CALCULATIONS!

It’s behavior is undefined – computers calculate in different order then humans.

GOOD:

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

CAN BE A PROBLEM (you can read it wrong):

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

WILL BE A PROBLEM:

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

EDIT:

I wrote same code in C++. Checked Assembler code and result is as I said:
Math with increment is not logic for people, but you can’t say that someone implemented it wrong.

As someone posted in comment:

line     #* E I O 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     #* E I O 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++);

Translated to 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

Answer:

Its because the ++ sign is an incremental operator to a variable. So your

$a = 1

$a++ = 2

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

Thats why it shows 3 as the answer.