3v4l.org

run code in 300+ PHP versions simultaneously
<?php # Arrow Functions 2.0 `Short Closures` # OLD WAY (PHP < 7.4 ) function cube($arg){ return ($arg * $arg * $arg); } $b = array_map('cube', range(1, 5)); var_dump('Short closure old syntax', $b); # OR $b = array_map(function($a2) {return ($a2 * $a2 * $a2);}, range(1, 5)); var_dump('Short closure old syntax variant', $b); # ONLY FOR PHP 7.4 + #/!\ MORE CLEAR PHP CODE BEST PRACTIVE /!\ $b = array_map(fn($n) => $n * $n * $n, range(1, 5)); var_dump('Arrow Function 7.4 + syntax', $b); # OLD Example with closure herit in parent scope with `use` syntax $factor = 10; $oldcalc = function(int $num) use ($factor): int { return $num * $factor; }; var_dump('Short closure old syntax', $oldcalc(2)); # PHP 7.4 + syntax it allows us to build more readable and maintainable code. $oldcalc = fn(int $num): int => $num * $factor; var_dump('Arrow Function 7.4 + syntax', $oldcalc(2));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/C3UfN
function name:  (null)
number of ops:  63
compiled vars:  !0 = $b, !1 = $factor, !2 = $oldcalc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   INIT_FCALL                                               'array_map'
          1        SEND_VAL                                                 'cube'
          2        INIT_FCALL                                               'range'
          3        SEND_VAL                                                 1
          4        SEND_VAL                                                 5
          5        DO_ICALL                                         $3      
          6        SEND_VAR                                                 $3
          7        DO_ICALL                                         $4      
          8        ASSIGN                                                   !0, $4
   10     9        INIT_FCALL                                               'var_dump'
         10        SEND_VAL                                                 'Short+closure+old+syntax'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                                 
   13    13        INIT_FCALL                                               'array_map'
         14        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
         15        SEND_VAL                                                 ~7
         16        INIT_FCALL                                               'range'
         17        SEND_VAL                                                 1
         18        SEND_VAL                                                 5
         19        DO_ICALL                                         $8      
         20        SEND_VAR                                                 $8
         21        DO_ICALL                                         $9      
         22        ASSIGN                                                   !0, $9
   14    23        INIT_FCALL                                               'var_dump'
         24        SEND_VAL                                                 'Short+closure+old+syntax+variant'
         25        SEND_VAR                                                 !0
         26        DO_ICALL                                                 
   18    27        INIT_FCALL                                               'array_map'
         28        DECLARE_LAMBDA_FUNCTION                          ~12     [1]
         29        SEND_VAL                                                 ~12
         30        INIT_FCALL                                               'range'
         31        SEND_VAL                                                 1
         32        SEND_VAL                                                 5
         33        DO_ICALL                                         $13     
         34        SEND_VAR                                                 $13
         35        DO_ICALL                                         $14     
         36        ASSIGN                                                   !0, $14
   19    37        INIT_FCALL                                               'var_dump'
         38        SEND_VAL                                                 'Arrow+Function+7.4+%2B+syntax'
         39        SEND_VAR                                                 !0
         40        DO_ICALL                                                 
   22    41        ASSIGN                                                   !1, 10
   23    42        DECLARE_LAMBDA_FUNCTION                          ~18     [2]
         43        BIND_LEXICAL                                             ~18, !1
         44        ASSIGN                                                   !2, ~18
   26    45        INIT_FCALL                                               'var_dump'
         46        SEND_VAL                                                 'Short+closure+old+syntax'
         47        INIT_DYNAMIC_CALL                                        !2
         48        SEND_VAL_EX                                              2
         49        DO_FCALL                                      0  $20     
         50        SEND_VAR                                                 $20
         51        DO_ICALL                                                 
   29    52        DECLARE_LAMBDA_FUNCTION                          ~22     [3]
         53        BIND_LEXICAL                                             ~22, !1
         54        ASSIGN                                                   !2, ~22
   30    55        INIT_FCALL                                               'var_dump'
         56        SEND_VAL                                                 'Arrow+Function+7.4+%2B+syntax'
         57        INIT_DYNAMIC_CALL                                        !2
         58        SEND_VAL_EX                                              2
         59        DO_FCALL                                      0  $24     
         60        SEND_VAR                                                 $24
         61        DO_ICALL                                                 
         62      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/C3UfN
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $a2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        MUL                                              ~1      !0, !0
          2        MUL                                              ~2      !0, ~1
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/C3UfN
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        MUL                                              ~1      !0, !0
          2        MUL                                              ~2      !0, ~1
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of Dynamic Function 1

Dynamic Function 2
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/C3UfN
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $num, !1 = $factor
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   24     2        MUL                                              ~2      !0, !1
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
   25     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of Dynamic Function 2

Dynamic Function 3
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/C3UfN
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $num, !1 = $factor
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        MUL                                              ~2      !0, !1
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
          5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of Dynamic Function 3

Function cube:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/C3UfN
function name:  cube
number of ops:  5
compiled vars:  !0 = $arg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    6     1        MUL                                              ~1      !0, !0
          2        MUL                                              ~2      !0, ~1
          3      > RETURN                                                   ~2
    7     4*     > RETURN                                                   null

End of function cube

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.87 ms | 1011 KiB | 16 Q