3v4l.org

run code in 300+ PHP versions simultaneously
<?php function hasValues(iterable &$value): bool { if (is_array($value)) { return (bool)$value; } if ($value instanceof \Iterator) { return $value->valid(); } $value = $value->getIterator(); return hasValues($value); } $weakMap = new WeakMap(); $x = new stdClass(); $y = new stdClass(); $weakMap[$x] = 'x'; $weakMap[$y] = 'y'; $data = [ [1, 2, 3], [], (fn () => yield from [1, 2, 3])(), (fn () => yield from [])(), new ArrayIterator([1, 2, 3]), new ArrayIterator([]), $weakMap, new WeakMap(), new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/tmp', RecursiveDirectoryIterator::SKIP_DOTS)), ]; foreach ($data as $d) { print_r([ 'value' => $d, 'hasValues' => var_export($hasValues = hasValues($d), true), // Once a generator is checked and does not have any values we can't attempt to iterate it anymore. 'values' => $hasValues ? (is_array($d) ? $d : iterator_to_array($d, false)) : 'null', ]); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 45, Position 2 = 74
Branch analysis from position: 45
2 jumps found. (Code = 78) Position 1 = 46, Position 2 = 74
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 69
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 62
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 74
filename:       /in/KDWtR
function name:  (null)
number of ops:  76
compiled vars:  !0 = $weakMap, !1 = $x, !2 = $y, !3 = $data, !4 = $d, !5 = $hasValues
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   NEW                                              $6      'WeakMap'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $6
   16     3        NEW                                              $9      'stdClass'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $9
   17     6        NEW                                              $12     'stdClass'
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !2, $12
   18     9        ASSIGN_DIM                                               !0, !1
         10        OP_DATA                                                  'x'
   19    11        ASSIGN_DIM                                               !0, !2
         12        OP_DATA                                                  'y'
   22    13        INIT_ARRAY                                       ~17     <array>
         14        ADD_ARRAY_ELEMENT                                ~17     <array>
   24    15        DECLARE_LAMBDA_FUNCTION                          ~18     [0]
         16        INIT_DYNAMIC_CALL                                        ~18
         17        DO_FCALL                                      0  $19     
         18        ADD_ARRAY_ELEMENT                                ~17     $19
   25    19        DECLARE_LAMBDA_FUNCTION                          ~20     [1]
         20        INIT_DYNAMIC_CALL                                        ~20
         21        DO_FCALL                                      0  $21     
         22        ADD_ARRAY_ELEMENT                                ~17     $21
   26    23        NEW                                              $22     'ArrayIterator'
   22    24        SEND_VAL_EX                                              <array>
   26    25        DO_FCALL                                      0          
   22    26        ADD_ARRAY_ELEMENT                                ~17     $22
   27    27        NEW                                              $24     'ArrayIterator'
   22    28        SEND_VAL_EX                                              <array>
   27    29        DO_FCALL                                      0          
   22    30        ADD_ARRAY_ELEMENT                                ~17     $24
   28    31        ADD_ARRAY_ELEMENT                                ~17     !0
   29    32        NEW                                              $26     'WeakMap'
         33        DO_FCALL                                      0          
         34        ADD_ARRAY_ELEMENT                                ~17     $26
   30    35        NEW                                              $28     'RecursiveIteratorIterator'
         36        NEW                                              $29     'RecursiveDirectoryIterator'
         37        SEND_VAL_EX                                              '%2Ftmp'
   22    38        SEND_VAL_EX                                              4096
   30    39        DO_FCALL                                      0          
   22    40        SEND_VAR_NO_REF_EX                                       $29
   30    41        DO_FCALL                                      0          
   22    42        ADD_ARRAY_ELEMENT                                ~17     $28
   21    43        ASSIGN                                                   !3, ~17
   33    44      > FE_RESET_R                                       $33     !3, ->74
         45    > > FE_FETCH_R                                               $33, !4, ->74
   34    46    >   INIT_FCALL                                               'print_r'
   35    47        INIT_ARRAY                                       ~34     !4, 'value'
   36    48        INIT_FCALL                                               'var_export'
         49        INIT_FCALL                                               'hasvalues'
         50        SEND_REF                                                 !4
         51        DO_FCALL                                      0  $35     
         52        ASSIGN                                           ~36     !5, $35
         53        SEND_VAL                                                 ~36
         54        SEND_VAL                                                 <true>
         55        DO_ICALL                                         $37     
         56        ADD_ARRAY_ELEMENT                                ~34     $37, 'hasValues'
   38    57      > JMPZ                                                     !5, ->69
         58    >   TYPE_CHECK                                  128          !4
         59      > JMPZ                                                     ~38, ->62
         60    >   QM_ASSIGN                                        ~39     !4
         61      > JMP                                                      ->67
         62    >   INIT_FCALL                                               'iterator_to_array'
         63        SEND_VAR                                                 !4
         64        SEND_VAL                                                 <false>
         65        DO_ICALL                                         $40     
         66        QM_ASSIGN                                        ~39     $40
         67    >   QM_ASSIGN                                        ~41     ~39
         68      > JMP                                                      ->70
         69    >   QM_ASSIGN                                        ~41     'null'
         70    >   ADD_ARRAY_ELEMENT                                ~34     ~41, 'values'
         71        SEND_VAL                                                 ~34
   34    72        DO_ICALL                                                 
   33    73      > JMP                                                      ->45
         74    >   FE_FREE                                                  $33
   40    75      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 161) Position 1 = -2
filename:       /in/KDWtR
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   GENERATOR_CREATE                                         
          1        YIELD_FROM                                       ~0      <array>
          2      > GENERATOR_RETURN                                         
          3*     > GENERATOR_RETURN                                         

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 161) Position 1 = -2
filename:       /in/KDWtR
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   GENERATOR_CREATE                                         
          1        YIELD_FROM                                       ~0      <array>
          2      > GENERATOR_RETURN                                         
          3*     > GENERATOR_RETURN                                         

End of Dynamic Function 1

Function hasvalues:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KDWtR
function name:  hasValues
number of ops:  22
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        TYPE_CHECK                                  128          !0
          2      > JMPZ                                                     ~1, ->6
    5     3    >   BOOL                                             ~2      !0
          4        VERIFY_RETURN_TYPE                                       ~2
          5      > RETURN                                                   ~2
    7     6    >   INSTANCEOF                                               !0, 'Iterator'
          7      > JMPZ                                                     ~3, ->12
    8     8    >   INIT_METHOD_CALL                                         !0, 'valid'
          9        DO_FCALL                                      0  $4      
         10        VERIFY_RETURN_TYPE                                       $4
         11      > RETURN                                                   $4
   11    12    >   INIT_METHOD_CALL                                         !0, 'getIterator'
         13        DO_FCALL                                      0  $5      
         14        ASSIGN                                                   !0, $5
   12    15        INIT_FCALL_BY_NAME                                       'hasValues'
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0  $7      
         18        VERIFY_RETURN_TYPE                                       $7
         19      > RETURN                                                   $7
   13    20*       VERIFY_RETURN_TYPE                                       
         21*     > RETURN                                                   null

End of function hasvalues

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.09 ms | 1011 KiB | 17 Q