3v4l.org

run code in 500+ PHP versions simultaneously
<?php function generator_chunks(\Generator $generator, $max_chunk_size) { $chunk = []; foreach ($generator as $item) { $chunk[] = $item; if (count($chunk) >= $max_chunk_size) { yield $chunk; $chunk = []; } } if ([] !== $chunk) { // Remaining chunk with fewer items. yield $chunk; } } function generator() { for ($i = 0; $i < 11; ++$i) { yield $i; } } foreach (generator_chunks(generator(), 3) as $chunk) { print json_encode($chunk) . "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 14
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/aSfeR
function name:  (null)
number of ops:  16
compiled vars:  !0 = $chunk
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   25     0  E >   INIT_FCALL                                                   'generator_chunks'
          1        INIT_FCALL                                                   'generator'
          2        DO_FCALL                                          0  $1      
          3        SEND_VAR                                                     $1
          4        SEND_VAL                                                     3
          5        DO_FCALL                                          0  $2      
          6      > FE_RESET_R                                           $3      $2, ->14
          7    > > FE_FETCH_R                                                   $3, !0, ->14
   26     8    >   INIT_FCALL                                                   'json_encode'
          9        SEND_VAR                                                     !0
         10        DO_ICALL                                             $4      
         11        CONCAT                                               ~5      $4, '%0A'
         12        ECHO                                                         ~5
   25    13      > JMP                                                          ->7
         14    >   FE_FREE                                                      $3
   27    15      > RETURN                                                       1

Function generator_chunks:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 14
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 18
Branch analysis from position: 14
filename:       /in/aSfeR
function name:  generator_chunks
number of ops:  19
compiled vars:  !0 = $generator, !1 = $max_chunk_size, !2 = $chunk, !3 = $item
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        GENERATOR_CREATE                                             
    5     3        ASSIGN                                                       !2, <array>
    6     4      > FE_RESET_R                                           $5      !0, ->14
          5    > > FE_FETCH_R                                                   $5, !3, ->14
    7     6    >   ASSIGN_DIM                                                   !2
          7        OP_DATA                                                      !3
    8     8        COUNT                                                ~7      !2
          9        IS_SMALLER_OR_EQUAL                                          !1, ~7
         10      > JMPZ                                                         ~8, ->13
    9    11    >   YIELD                                                        !2
   10    12        ASSIGN                                                       !2, <array>
    6    13    > > JMP                                                          ->5
         14    >   FE_FREE                                                      $5
   13    15        IS_NOT_IDENTICAL                                             !2, <array>
         16      > JMPZ                                                         ~11, ->18
   15    17    >   YIELD                                                        !2
   17    18    > > GENERATOR_RETURN                                             

End of function generator_chunks

Function generator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 3
Branch analysis from position: 7
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 3
Branch analysis from position: 7
Branch analysis from position: 3
filename:       /in/aSfeR
function name:  generator
number of ops:  8
compiled vars:  !0 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   GENERATOR_CREATE                                             
   20     1        ASSIGN                                                       !0, 0
          2      > JMP                                                          ->5
   21     3    >   YIELD                                                        !0
   20     4        PRE_INC                                                      !0
          5    >   IS_SMALLER                                                   !0, 11
          6      > JMPNZ                                                        ~4, ->3
   23     7    > > GENERATOR_RETURN                                             

End of function generator

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
134.04 ms | 2541 KiB | 16 Q