3v4l.org

run code in 300+ PHP versions simultaneously
<?php // WARNING: this is just a way to experiment with short closures and you SHOULD NOT write code like this echo '# PART 1 - Short closures may contain only 1 expression and nothing more'.PHP_EOL.PHP_EOL; // Two expressions: Syntax error (not catchable in this branch) // $test = fn($x) => { $y = 10; return $x + $y; }; function last(...$args) { // all expression results enter here return end($args); // only last one is returned } // unlike JavaScript where the last expression is returned, PHP returns a boolean $andOperator = fn($x) => ($y = 10) && $x + $y; var_dump($andOperator(7)); // PHP also doesn't have a comma operator like JS. In JS "a, b" will return b // $commaOperator = fn($x) => $y = 10, $x + $y; // Syntax error // end() only accepts references, so no luck yet: //$arrayEnd = fn($x) => end([$y = 10, $x + $y]); // Fatal error: Only variables can be passed by reference $multipleLines = fn($x) => last( $y = $x * 10, $z = $y + 15, $x + $y + $z // result of this line is returned from last() ); var_dump($multipleLines(7)); $moreComplex = fn($x) => last( $numbers = range(0, $x), $y = array_map(fn($z) => strlen($z), $numbers), array_sum($y) // result of this line is returned from last() ); var_dump($moreComplex(7)); echo PHP_EOL; echo '# PART 2 - Short closures bind by value, always'.PHP_EOL.PHP_EOL; function walkThisWay(Closure $closure) { // just an example $i = 0; while ($word = $closure()) { echo $word.PHP_EOL; $i++; if ($i > 3) { // length of $parameters echo 'NAH! it seems it\'s not really a reference'.PHP_EOL; break; } } echo PHP_EOL; } echo "## TEST 1 - not a reference:".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; // we want to change $parameters, but what we have is a copy of it, arrays behave this way walkThisWay(fn() => array_shift($parameters)); echo "## TEST 2 - syntax error:".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; // Error is not catchable in this branch //walkThisWay(fn() use (&$parameters) => array_shift($parameters)); echo PHP_EOL; echo "## TEST 3 - object:".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; $wrapper = (object) ['parameters' => &$parameters]; // objects are not really references, but they know where all data is, a copy is not made // You could also use ArayObject instead of $wrapper walkThisWay(fn() => array_shift($wrapper->parameters)); echo "## TEST 4 - functional hack:".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; function ref(&$reference, Closure $closure) { return function (...$args) use ($closure, &$reference) { $args[] = &$reference; // add a reference to $parameters in the arguments return $closure(...$args); }; } walkThisWay(ref($parameters, fn(&$parameters) => array_shift($parameters))); echo "## TEST 5 - good old functions (better use this version in real code):".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; walkThisWay(function () use (&$parameters) { return array_shift($parameters); });
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  (null)
number of ops:  71
compiled vars:  !0 = $andOperator, !1 = $y, !2 = $multipleLines, !3 = $z, !4 = $moreComplex, !5 = $numbers, !6 = $parameters, !7 = $wrapper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ECHO                                                     '%23+PART+1+-+Short+closures+may+contain+only+1+expression+and+nothing+more%0A%0A'
   15     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A15%240'
          2        BIND_LEXICAL                                             ~8, !1
          3        ASSIGN                                                   !0, ~8
   16     4        INIT_FCALL                                               'var_dump'
          5        INIT_DYNAMIC_CALL                                        !0
          6        SEND_VAL_EX                                              7
          7        DO_FCALL                                      0  $10     
          8        SEND_VAR                                                 $10
          9        DO_ICALL                                                 
   24    10        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A24%241'
         11        BIND_LEXICAL                                             ~12, !1
         12        BIND_LEXICAL                                             ~12, !3
         13        ASSIGN                                                   !2, ~12
   29    14        INIT_FCALL                                               'var_dump'
         15        INIT_DYNAMIC_CALL                                        !2
         16        SEND_VAL_EX                                              7
         17        DO_FCALL                                      0  $14     
         18        SEND_VAR                                                 $14
         19        DO_ICALL                                                 
   31    20        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A31%242'
         21        BIND_LEXICAL                                             ~16, !5
         22        BIND_LEXICAL                                             ~16, !1
         23        BIND_LEXICAL                                             ~16, !3
         24        ASSIGN                                                   !4, ~16
   36    25        INIT_FCALL                                               'var_dump'
         26        INIT_DYNAMIC_CALL                                        !4
         27        SEND_VAL_EX                                              7
         28        DO_FCALL                                      0  $18     
         29        SEND_VAR                                                 $18
         30        DO_ICALL                                                 
   38    31        ECHO                                                     '%0A'
   40    32        ECHO                                                     '%23+PART+2+-+Short+closures+bind+by+value%2C+always%0A%0A'
   55    33        ECHO                                                     '%23%23+TEST+1+-+not+a+reference%3A%0A'
   56    34        ASSIGN                                                   !6, <array>
   58    35        INIT_FCALL                                               'walkthisway'
         36        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A58%244'
         37        BIND_LEXICAL                                             ~21, !6
         38        SEND_VAL                                                 ~21
         39        DO_FCALL                                      0          
   60    40        ECHO                                                     '%23%23+TEST+2+-+syntax+error%3A%0A'
   61    41        ASSIGN                                                   !6, <array>
   64    42        ECHO                                                     '%0A'
   66    43        ECHO                                                     '%23%23+TEST+3+-+object%3A%0A'
   67    44        ASSIGN                                                   !6, <array>
   68    45        INIT_ARRAY                                       ~25     !6, 'parameters'
         46        CAST                                          8  ~26     ~25
         47        ASSIGN                                                   !7, ~26
   71    48        INIT_FCALL                                               'walkthisway'
         49        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A71%245'
         50        BIND_LEXICAL                                             ~28, !7
         51        SEND_VAL                                                 ~28
         52        DO_FCALL                                      0          
   73    53        ECHO                                                     '%23%23+TEST+4+-+functional+hack%3A%0A'
   74    54        ASSIGN                                                   !6, <array>
   81    55        INIT_FCALL                                               'walkthisway'
         56        INIT_FCALL                                               'ref'
         57        SEND_REF                                                 !6
         58        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A81%247'
         59        SEND_VAL                                                 ~31
         60        DO_FCALL                                      0  $32     
         61        SEND_VAR                                                 $32
         62        DO_FCALL                                      0          
   83    63        ECHO                                                     '%23%23+TEST+5+-+good+old+functions+%28better+use+this+version+in+real+code%29%3A%0A'
   84    64        ASSIGN                                                   !6, <array>
   85    65        INIT_FCALL                                               'walkthisway'
         66        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A85%248'
         67        BIND_LEXICAL                                             ~35, !6
         68        SEND_VAL                                                 ~35
         69        DO_FCALL                                      0          
         70      > RETURN                                                   1

Function last:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  last
number of ops:  6
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV_VARIADIC                                    !0      
   11     1        INIT_FCALL                                               'end'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
   12     5*     > RETURN                                                   null

End of function last

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A15%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/Djd0B
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $x, !1 = $y
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        ASSIGN                                           ~2      !1, 10
          3      > JMPZ_EX                                          ~2      ~2, ->6
          4    >   ADD                                              ~3      !0, !1
          5        BOOL                                             ~2      ~3
          6    > > RETURN                                                   ~2
          7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A15%240

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A24%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  {closure}
number of ops:  16
compiled vars:  !0 = $x, !1 = $y, !2 = $z
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        INIT_FCALL                                               'last'
   25     4        MUL                                              ~3      !0, 10
          5        ASSIGN                                           ~4      !1, ~3
          6        SEND_VAL                                                 ~4
   26     7        ADD                                              ~5      !1, 15
          8        ASSIGN                                           ~6      !2, ~5
          9        SEND_VAL                                                 ~6
   27    10        ADD                                              ~7      !0, !1
         11        ADD                                              ~8      ~7, !2
         12        SEND_VAL                                                 ~8
         13        DO_FCALL                                      0  $9      
         14      > RETURN                                                   $9
   28    15*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A24%241

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A31%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  {closure}
number of ops:  25
compiled vars:  !0 = $x, !1 = $numbers, !2 = $y, !3 = $z
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
          4        INIT_FCALL                                               'last'
   32     5        INIT_FCALL                                               'range'
          6        SEND_VAL                                                 0
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $4      
          9        ASSIGN                                           ~5      !1, $4
         10        SEND_VAL                                                 ~5
   33    11        INIT_FCALL                                               'array_map'
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A33%243'
         13        SEND_VAL                                                 ~6
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $7      
         16        ASSIGN                                           ~8      !2, $7
         17        SEND_VAL                                                 ~8
   34    18        INIT_FCALL                                               'array_sum'
         19        SEND_VAR                                                 !2
         20        DO_ICALL                                         $9      
         21        SEND_VAR                                                 $9
         22        DO_FCALL                                      0  $10     
         23      > RETURN                                                   $10
   35    24*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A31%242

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A33%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $z
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        STRLEN                                           ~1      !0
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A33%243

Function walkthisway:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 3
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 10
filename:       /in/Djd0B
function name:  walkThisWay
number of ops:  16
compiled vars:  !0 = $closure, !1 = $i, !2 = $word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   43     1        ASSIGN                                                   !1, 0
   44     2      > JMP                                                      ->10
   45     3    >   CONCAT                                           ~4      !2, '%0A'
          4        ECHO                                                     ~4
   46     5        PRE_INC                                                  !1
   47     6        IS_SMALLER                                               3, !1
          7      > JMPZ                                                     ~6, ->10
   48     8    >   ECHO                                                     'NAH%21+it+seems+it%27s+not+really+a+reference%0A'
   49     9      > JMP                                                      ->14
   44    10    >   INIT_DYNAMIC_CALL                                        !0
         11        DO_FCALL                                      0  $7      
         12        ASSIGN                                           ~8      !2, $7
         13      > JMPNZ                                                    ~8, ->3
   52    14    >   ECHO                                                     '%0A'
   53    15      > RETURN                                                   null

End of function walkthisway

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A58%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $parameters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   BIND_STATIC                                              !0
          1        INIT_FCALL                                               'array_shift'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
          5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A58%244

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A71%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $wrapper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   BIND_STATIC                                              !0
          1        INIT_FCALL                                               'array_shift'
          2        FETCH_OBJ_W                                      $1      !0, 'parameters'
          3        SEND_REF                                                 $1
          4        DO_ICALL                                         $2      
          5      > RETURN                                                   $2
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A71%245

Function ref:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  ref
number of ops:  7
compiled vars:  !0 = $reference, !1 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   76     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDjd0B%3A76%246'
          3        BIND_LEXICAL                                             ~2, !1
          4        BIND_LEXICAL                                             ~2, !0
   79     5      > RETURN                                                   ~2
   80     6*     > RETURN                                                   null

End of function ref

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A76%246:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $args, !1 = $closure, !2 = $reference
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV_VARIADIC                                    !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   77     3        FETCH_DIM_W                                      $3      !0
          4        ASSIGN_REF                                               $3, !2
   78     5        INIT_DYNAMIC_CALL                                        !1
          6        SEND_UNPACK                                              !0
          7        CHECK_UNDEF_ARGS                                         
          8        DO_FCALL                                      1  $5      
          9      > RETURN                                                   $5
   79    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A76%246

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A81%247:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $parameters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
          1        INIT_FCALL                                               'array_shift'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
          5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A81%247

Function %00%7Bclosure%7D%2Fin%2FDjd0B%3A85%248:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Djd0B
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $parameters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   BIND_STATIC                                              !0
          1        INIT_FCALL                                               'array_shift'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
          5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDjd0B%3A85%248

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.09 ms | 1419 KiB | 32 Q