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))))
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
test1:5 test2:5 test3:9 test4:3 test5:3 test6:3 quiz:4445
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 5.1.1 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.24
test1:5 test2:6 test3:10 test4:3 test5:3 test6:3 quiz:4556
Output for 5.1.0
Fatal error: fatal flex scanner internal error--end of buffer missed in /in/HFmA7 on line 130
Process exited with code 255.
Output for 4.3.10 - 4.3.11, 4.4.0 - 4.4.9, 5.0.2 - 5.0.5
test1:5 test2:6 test3:10 test4:2 test5:2 test6:3 quiz:4556
Output for 4.3.0 - 4.3.9, 5.0.0 - 5.0.1
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HFmA7 on line 4 PHP_EOLtest1:5 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HFmA7 on line 12 PHP_EOLtest2:6 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HFmA7 on line 21 PHP_EOLtest3:10 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HFmA7 on line 30 PHP_EOLtest4:2 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HFmA7 on line 39 PHP_EOLtest5:2 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HFmA7 on line 49 PHP_EOLtest6:3 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/HFmA7 on line 90 PHP_EOLquiz:4556

preferences:
201.46 ms | 415 KiB | 5 Q