3v4l.org

run code in 300+ PHP versions simultaneously
<?php // #1 function test1(){ echo PHP_EOL.__function__ .":"; $a=1; echo (++$a)+(++$a); } test1(); // 5 // #2 function test2(){ echo PHP_EOL.__function__ .":"; $a=1; $b=&$a; echo (++$a)+(++$a); } test2(); // 6(5.1.1 - 5.6.24) or 5(hhvm, 7.0.0+) // #3 function test3(){ echo PHP_EOL.__function__ .":"; $a=1; $b=&$a; echo (++$a)+(++$a)+(++$a); } test3(); // 10(5.1.1 - 5.6.24) or 9(hhvm, 7.0.0+) // #4 function test4(){ echo PHP_EOL.__function__ .":"; $a=1; // $b=&$a; echo $a + $a++; } test4(); // 3 // #5 function test5(){ echo PHP_EOL.__function__ .":"; $a = 1; // $b = &$a; // echo @$a + $a++; // 2 echo $a + $a++; // 3 } test5(); // 3 // #6 function test6(){ echo PHP_EOL.__function__ .":"; $a = 1; echo $a + $a + $a++; } test6(); // 3 /* 从上面的结果来看,有以下三点必须知识: - ++$a 是自己加1,之后的都要加1;如果是指针引用,有些版本会导致 ++$a 的临时值为自身,让之前的计算结果也改变。 - $a++ 是自己不用增1的结果,但是这个操作符之后的都要加1,不管有没有指针引用。 - 编译器每一次都要把表达式组合成两元组:((($a++ + $a) + $a) + $a); 但是在 ($a + $a++) 的情况下,会改变顺序为 ($a++ + $a),所以 $a++ + $a 的结果是3. 最终版本(php v7.0)(`$a = 1; $b = &$a; echo ++$a + ++$a + $a;`) compiled vars: !0 = $a, !1 = $b line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 2 0 E > ASSIGN !0, 1 3 1 ASSIGN_REF !1, !0 4 2 PRE_INC $4 !0 3 PRE_INC $5 !0 4 ADD ~6 $4, $5 5 ADD ~7 ~6, !0 6 ECHO ~7 5 7 ECHO !0 8 > RETURN 1 from: https://3v4l.org/fgLZO/vld#tabs 注意: > 不同的php 版本不同,导致的结果不同。我用 c++ 测试过,gcc 和 clang 的结果也不同(当然会有warning). 总结: - 目前hhvm和7.0.0+都不再会受到指针引用的影响。(受影响的版本 5.1.1 - 5.6.24) - `++a;++a`, clang 结果是6, gcc 结果是5 - 混写的写法:不易查错,不易 debug,全语言混乱 - 建议:所有自增全部用新行写 */ // four quiz function quiz() { echo PHP_EOL.__function__ .":"; $a=1; $b=&$a; echo ($a++)+(++$a); $a=1; $b=&$a; echo (++$a)+($a++); $a=1; //$b=&$a; echo (++$a)+($a++); $a=1; $b=&$a; echo (++$a)+(++$a); } quiz(); // answer: 4445(hvvm,7.0.0+) 4556(4.3.0 - 5.6.24) // Good points from above // #0.1 // $a = -1; // var_dump($a && true); // true // var_dump($a && false); // false // var_dump($a && -1); // true // #0.2 // $a + $a + $a + $a + $a // + is left-assoc, so it's grouped as // (((($a + $a) + $a) + $a) + $a) // #0.3 // $a + $a + $a + $a + $a // $a = $b = $c = $d = $e // = is right-assoc, so it's grouped as // ($a = ($b = ($c = ($d = $e))))
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HFmA7
function name:  (null)
number of ops:  15
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   INIT_FCALL                                               'test1'
          1        DO_FCALL                                      0          
   17     2        INIT_FCALL                                               'test2'
          3        DO_FCALL                                      0          
   26     4        INIT_FCALL                                               'test3'
          5        DO_FCALL                                      0          
   35     6        INIT_FCALL                                               'test4'
          7        DO_FCALL                                      0          
   45     8        INIT_FCALL                                               'test5'
          9        DO_FCALL                                      0          
   53    10        INIT_FCALL                                               'test6'
         11        DO_FCALL                                      0          
  108    12        INIT_FCALL                                               'quiz'
         13        DO_FCALL                                      0          
  130    14      > RETURN                                                   1

Function test1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HFmA7
function name:  test1
number of ops:  7
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ECHO                                                     '%0Atest1%3A'
    5     1        ASSIGN                                                   !0, 1
    6     2        PRE_INC                                          ~2      !0
          3        PRE_INC                                          ~3      !0
          4        ADD                                              ~4      ~2, ~3
          5        ECHO                                                     ~4
    7     6      > RETURN                                                   null

End of function test1

Function test2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HFmA7
function name:  test2
number of ops:  8
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   ECHO                                                     '%0Atest2%3A'
   13     1        ASSIGN                                                   !0, 1
   14     2        ASSIGN_REF                                               !1, !0
   15     3        PRE_INC                                          ~4      !0
          4        PRE_INC                                          ~5      !0
          5        ADD                                              ~6      ~4, ~5
          6        ECHO                                                     ~6
   16     7      > RETURN                                                   null

End of function test2

Function test3:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HFmA7
function name:  test3
number of ops:  10
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   ECHO                                                     '%0Atest3%3A'
   22     1        ASSIGN                                                   !0, 1
   23     2        ASSIGN_REF                                               !1, !0
   24     3        PRE_INC                                          ~4      !0
          4        PRE_INC                                          ~5      !0
          5        ADD                                              ~6      ~4, ~5
          6        PRE_INC                                          ~7      !0
          7        ADD                                              ~8      ~6, ~7
          8        ECHO                                                     ~8
   25     9      > RETURN                                                   null

End of function test3

Function test4:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HFmA7
function name:  test4
number of ops:  6
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   ECHO                                                     '%0Atest4%3A'
   31     1        ASSIGN                                                   !0, 1
   33     2        POST_INC                                         ~2      !0
          3        ADD                                              ~3      !0, ~2
          4        ECHO                                                     ~3
   34     5      > RETURN                                                   null

End of function test4

Function test5:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HFmA7
function name:  test5
number of ops:  6
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   ECHO                                                     '%0Atest5%3A'
   40     1        ASSIGN                                                   !0, 1
   43     2        POST_INC                                         ~2      !0
          3        ADD                                              ~3      !0, ~2
          4        ECHO                                                     ~3
   44     5      > RETURN                                                   null

End of function test5

Function test6:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HFmA7
function name:  test6
number of ops:  7
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   ECHO                                                     '%0Atest6%3A'
   50     1        ASSIGN                                                   !0, 1
   51     2        ADD                                              ~2      !0, !0
          3        POST_INC                                         ~3      !0
          4        ADD                                              ~4      ~2, ~3
          5        ECHO                                                     ~4
   52     6      > RETURN                                                   null

End of function test6

Function quiz:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HFmA7
function name:  quiz
number of ops:  25
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   ECHO                                                     '%0Aquiz%3A'
   92     1        ASSIGN                                                   !0, 1
   93     2        ASSIGN_REF                                               !1, !0
   94     3        POST_INC                                         ~4      !0
          4        PRE_INC                                          ~5      !0
          5        ADD                                              ~6      ~4, ~5
          6        ECHO                                                     ~6
   96     7        ASSIGN                                                   !0, 1
   97     8        ASSIGN_REF                                               !1, !0
   98     9        PRE_INC                                          ~9      !0
         10        POST_INC                                         ~10     !0
         11        ADD                                              ~11     ~9, ~10
         12        ECHO                                                     ~11
  100    13        ASSIGN                                                   !0, 1
  102    14        PRE_INC                                          ~13     !0
         15        POST_INC                                         ~14     !0
         16        ADD                                              ~15     ~13, ~14
         17        ECHO                                                     ~15
  104    18        ASSIGN                                                   !0, 1
  105    19        ASSIGN_REF                                               !1, !0
  106    20        PRE_INC                                          ~18     !0
         21        PRE_INC                                          ~19     !0
         22        ADD                                              ~20     ~18, ~19
         23        ECHO                                                     ~20
  107    24      > RETURN                                                   null

End of function quiz

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
280.99 ms | 1017 KiB | 20 Q