3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getPortion($products, $page, $per_page) { $index = $page * $per_page; // to get always positive module $real_index = ($index % sizeof($products) + sizeof($products)) % sizeof($products); $return = array_slice($products, $real_index, $per_page); while (sizeof($return) < $per_page) { $return = array_merge($return, array_slice($products, 0, $per_page - sizeof($return))); } return $return; } $products = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'); // 0 page echo json_encode(getPortion($products, 0, 3)); // ["a","b","c"] // 3 page echo json_encode(getPortion($products, 3, 3)); // ["j","k","a"] // -1 page echo json_encode(getPortion($products, -1, 3)); // ["i","j","k"] // 0 page, but per_page larger than array length. echo json_encode(getPortion(array('a', 'b', 'c'), 0, 5)); // ["a","b","c","a","b"]
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/l8JGB
function name:  (null)
number of ops:  38
compiled vars:  !0 = $products
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   ASSIGN                                                   !0, <array>
   21     1        INIT_FCALL                                               'json_encode'
          2        INIT_FCALL                                               'getportion'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        SEND_VAL                                                 3
          6        DO_FCALL                                      0  $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                         $3      
          9        ECHO                                                     $3
   25    10        INIT_FCALL                                               'json_encode'
         11        INIT_FCALL                                               'getportion'
         12        SEND_VAR                                                 !0
         13        SEND_VAL                                                 3
         14        SEND_VAL                                                 3
         15        DO_FCALL                                      0  $4      
         16        SEND_VAR                                                 $4
         17        DO_ICALL                                         $5      
         18        ECHO                                                     $5
   29    19        INIT_FCALL                                               'json_encode'
         20        INIT_FCALL                                               'getportion'
         21        SEND_VAR                                                 !0
         22        SEND_VAL                                                 -1
         23        SEND_VAL                                                 3
         24        DO_FCALL                                      0  $6      
         25        SEND_VAR                                                 $6
         26        DO_ICALL                                         $7      
         27        ECHO                                                     $7
   33    28        INIT_FCALL                                               'json_encode'
         29        INIT_FCALL                                               'getportion'
         30        SEND_VAL                                                 <array>
         31        SEND_VAL                                                 0
         32        SEND_VAL                                                 5
         33        DO_FCALL                                      0  $8      
         34        SEND_VAR                                                 $8
         35        DO_ICALL                                         $9      
         36        ECHO                                                     $9
   34    37      > RETURN                                                   1

Function getportion:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 19
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 19
Branch analysis from position: 34
Branch analysis from position: 19
filename:       /in/l8JGB
function name:  getPortion
number of ops:  36
compiled vars:  !0 = $products, !1 = $page, !2 = $per_page, !3 = $index, !4 = $real_index, !5 = $return
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    5     3        MUL                                              ~6      !1, !2
          4        ASSIGN                                                   !3, ~6
    8     5        COUNT                                            ~8      !0
          6        MOD                                              ~9      !3, ~8
          7        COUNT                                            ~10     !0
          8        ADD                                              ~11     ~9, ~10
          9        COUNT                                            ~12     !0
         10        MOD                                              ~13     ~11, ~12
         11        ASSIGN                                                   !4, ~13
   10    12        INIT_FCALL                                               'array_slice'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !4
         15        SEND_VAR                                                 !2
         16        DO_ICALL                                         $15     
         17        ASSIGN                                                   !5, $15
   11    18      > JMP                                                      ->31
   13    19    >   INIT_FCALL                                               'array_merge'
         20        SEND_VAR                                                 !5
         21        INIT_FCALL                                               'array_slice'
         22        SEND_VAR                                                 !0
         23        SEND_VAL                                                 0
         24        COUNT                                            ~17     !5
         25        SUB                                              ~18     !2, ~17
         26        SEND_VAL                                                 ~18
         27        DO_ICALL                                         $19     
         28        SEND_VAR                                                 $19
         29        DO_ICALL                                         $20     
         30        ASSIGN                                                   !5, $20
   11    31    >   COUNT                                            ~22     !5
         32        IS_SMALLER                                               ~22, !2
         33      > JMPNZ                                                    ~23, ->19
   15    34    > > RETURN                                                   !5
   16    35*     > RETURN                                                   null

End of function getportion

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.65 ms | 1394 KiB | 23 Q