2

Here's my code:

$first = 10;
$_temp1 = &$first; // important!
$result1 = (++$first) + (++$first); 
var_dump($result1); // the result1's value is 24

$second = 10;
$result2 = (++$second) + (++$second); 
var_dump($result2); // the result2's value is 23

$third = 10;
$_temp3 = &$third; // important!
$result3 = (++$third) + ($third++); 
var_dump($result3); // the result3's value is 23

I can not understand why result1 and result3 are affected by the reference.

Could someone explain the reason about this?

Tamara
  • 186
  • 7
  • http://stackoverflow.com/questions/6175316/the-difference-between-var-and-var As a start – ntgCleaner Jul 05 '16 at 16:36
  • 3
    mixing multiple `++`/`--` operators in a single line is undefined behavior, and you results will be essentially "random". – Marc B Jul 05 '16 at 16:39
  • Why does, PHP 5 vs PHP 7 prints different result? https://3v4l.org/i7Ulb – Chay22 Jul 05 '16 at 16:42
  • 3
    because it's undefined behavior. if the parser changes how it executes things internally, then you get different results. php7 is a highly different beast internally than php5. – Marc B Jul 05 '16 at 16:45
  • when you make a variable pass by reference, to my knowledge the final value of the variable is always used while doing any operations. But that is not the case with normal variables, as PHP will be able to allocate memory for the operation and treat them separately – Dharam Jul 05 '16 at 17:15

0 Answers0