3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generator(): iterable { $count = -1; while (true) { echo 'Stopping: ', $count, \PHP_EOL; yield ++$count; echo 'Resuming: ', $count, \PHP_EOL; } } function limit(\Iterator $inner, int $count) { if (1 > $count) return; $inner->rewind(); while ($inner->valid()) { if (!$count--) { return; } yield $inner->key() => $inner->current(); $inner->next(); } } echo "Construct:\n"; $i = limit(generator(), 3); echo "Rewind:\n"; $i->rewind(); echo "\n1st iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next(); echo "\n2nd iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next(); echo "\n3rd iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next(); echo "\n4th iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next(); echo "\n5th iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next();
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 47
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 62
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 77
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 62
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 77
Branch analysis from position: 75
Branch analysis from position: 77
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 62
Branch analysis from position: 60
Branch analysis from position: 62
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 47
Branch analysis from position: 45
Branch analysis from position: 47
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
Branch analysis from position: 32
filename:       /in/BS5HF
function name:  (null)
number of ops:  87
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   ECHO                                                     'Construct%3A%0A'
   24     1        INIT_FCALL                                               'limit'
          2        INIT_FCALL                                               'generator'
          3        DO_FCALL                                      0  $1      
          4        SEND_VAR                                                 $1
          5        SEND_VAL                                                 3
          6        DO_FCALL                                      0  $2      
          7        ASSIGN                                                   !0, $2
   25     8        ECHO                                                     'Rewind%3A%0A'
   26     9        INIT_METHOD_CALL                                         !0, 'rewind'
         10        DO_FCALL                                      0          
   28    11        ECHO                                                     '%0A1st+iteration%0A'
   29    12        INIT_METHOD_CALL                                         !0, 'valid'
         13        DO_FCALL                                      0  $5      
         14      > JMPZ                                                     $5, ->17
         15    >   QM_ASSIGN                                        ~6      'Valid%0A'
         16      > JMP                                                      ->18
         17    >   QM_ASSIGN                                        ~6      'Not+valid%0A'
         18    >   ECHO                                                     ~6
   30    19        ECHO                                                     'Current+value%3A+'
         20        INIT_METHOD_CALL                                         !0, 'current'
         21        DO_FCALL                                      0  $7      
         22        ECHO                                                     $7
         23        ECHO                                                     '%0A'
   31    24        INIT_METHOD_CALL                                         !0, 'next'
         25        DO_FCALL                                      0          
   33    26        ECHO                                                     '%0A2nd+iteration%0A'
   34    27        INIT_METHOD_CALL                                         !0, 'valid'
         28        DO_FCALL                                      0  $9      
         29      > JMPZ                                                     $9, ->32
         30    >   QM_ASSIGN                                        ~10     'Valid%0A'
         31      > JMP                                                      ->33
         32    >   QM_ASSIGN                                        ~10     'Not+valid%0A'
         33    >   ECHO                                                     ~10
   35    34        ECHO                                                     'Current+value%3A+'
         35        INIT_METHOD_CALL                                         !0, 'current'
         36        DO_FCALL                                      0  $11     
         37        ECHO                                                     $11
         38        ECHO                                                     '%0A'
   36    39        INIT_METHOD_CALL                                         !0, 'next'
         40        DO_FCALL                                      0          
   38    41        ECHO                                                     '%0A3rd+iteration%0A'
   39    42        INIT_METHOD_CALL                                         !0, 'valid'
         43        DO_FCALL                                      0  $13     
         44      > JMPZ                                                     $13, ->47
         45    >   QM_ASSIGN                                        ~14     'Valid%0A'
         46      > JMP                                                      ->48
         47    >   QM_ASSIGN                                        ~14     'Not+valid%0A'
         48    >   ECHO                                                     ~14
   40    49        ECHO                                                     'Current+value%3A+'
         50        INIT_METHOD_CALL                                         !0, 'current'
         51        DO_FCALL                                      0  $15     
         52        ECHO                                                     $15
         53        ECHO                                                     '%0A'
   41    54        INIT_METHOD_CALL                                         !0, 'next'
         55        DO_FCALL                                      0          
   43    56        ECHO                                                     '%0A4th+iteration%0A'
   44    57        INIT_METHOD_CALL                                         !0, 'valid'
         58        DO_FCALL                                      0  $17     
         59      > JMPZ                                                     $17, ->62
         60    >   QM_ASSIGN                                        ~18     'Valid%0A'
         61      > JMP                                                      ->63
         62    >   QM_ASSIGN                                        ~18     'Not+valid%0A'
         63    >   ECHO                                                     ~18
   45    64        ECHO                                                     'Current+value%3A+'
         65        INIT_METHOD_CALL                                         !0, 'current'
         66        DO_FCALL                                      0  $19     
         67        ECHO                                                     $19
         68        ECHO                                                     '%0A'
   46    69        INIT_METHOD_CALL                                         !0, 'next'
         70        DO_FCALL                                      0          
   48    71        ECHO                                                     '%0A5th+iteration%0A'
   49    72        INIT_METHOD_CALL                                         !0, 'valid'
         73        DO_FCALL                                      0  $21     
         74      > JMPZ                                                     $21, ->77
         75    >   QM_ASSIGN                                        ~22     'Valid%0A'
         76      > JMP                                                      ->78
         77    >   QM_ASSIGN                                        ~22     'Not+valid%0A'
         78    >   ECHO                                                     ~22
   50    79        ECHO                                                     'Current+value%3A+'
         80        INIT_METHOD_CALL                                         !0, 'current'
         81        DO_FCALL                                      0  $23     
         82        ECHO                                                     $23
         83        ECHO                                                     '%0A'
   51    84        INIT_METHOD_CALL                                         !0, 'next'
         85        DO_FCALL                                      0          
         86      > RETURN                                                   1

Function generator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 3
Branch analysis from position: 12
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 3
Branch analysis from position: 12
Branch analysis from position: 3
filename:       /in/BS5HF
function name:  generator
number of ops:  13
compiled vars:  !0 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   GENERATOR_CREATE                                         
    3     1        ASSIGN                                                   !0, -1
    4     2      > JMP                                                      ->11
    5     3    >   ECHO                                                     'Stopping%3A+'
          4        ECHO                                                     !0
          5        ECHO                                                     '%0A'
    6     6        PRE_INC                                          ~2      !0
          7        YIELD                                                    ~2
    7     8        ECHO                                                     'Resuming%3A+'
          9        ECHO                                                     !0
         10        ECHO                                                     '%0A'
    4    11    > > JMPNZ                                                    <true>, ->3
    9    12    > > GENERATOR_RETURN                                         

End of function generator

Function limit:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 9
Branch analysis from position: 23
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 9
Branch analysis from position: 23
Branch analysis from position: 9
filename:       /in/BS5HF
function name:  limit
number of ops:  24
compiled vars:  !0 = $inner, !1 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
   12     3        IS_SMALLER                                               !1, 1
          4      > JMPZ                                                     ~2, ->6
          5    > > GENERATOR_RETURN                                         
   13     6    >   INIT_METHOD_CALL                                         !0, 'rewind'
          7        DO_FCALL                                      0          
   14     8      > JMP                                                      ->20
   15     9    >   POST_DEC                                         ~4      !1
         10        BOOL_NOT                                         ~5      ~4
         11      > JMPZ                                                     ~5, ->13
   16    12    > > GENERATOR_RETURN                                         
   18    13    >   INIT_METHOD_CALL                                         !0, 'key'
         14        DO_FCALL                                      0  $6      
         15        INIT_METHOD_CALL                                         !0, 'current'
         16        DO_FCALL                                      0  $7      
         17        YIELD                                                    $7, $6
   19    18        INIT_METHOD_CALL                                         !0, 'next'
         19        DO_FCALL                                      0          
   14    20    >   INIT_METHOD_CALL                                         !0, 'valid'
         21        DO_FCALL                                      0  $10     
         22      > JMPNZ                                                    $10, ->9
   21    23    > > GENERATOR_RETURN                                         

End of function limit

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
126.62 ms | 1415 KiB | 15 Q