3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getKey($current, $step): int { $keys = [1,2,3,4,5,6,7]; $currentKey = array_search($current, $keys); $size = count($keys); // normalize step offset $step = ($step % $size) + $size; // ^-----------^ ^-----^ // │ └ move possible negative to positive // └ get a value from -7 to +7 // add offset for current key $newKey = ($currentKey + $step) % $size; // ^-----------------^ ^-----^ // │ └ wrap around in case we exceed $size // └ add normalized step offset to new element to current return $keys[$newKey]; } // Tests echo getKey(1,-1) . PHP_EOL; echo getKey(3,1) . PHP_EOL; echo getKey(7,1) . PHP_EOL; echo getKey(7,15) . PHP_EOL; // same as +1 echo getKey(1,-8) . PHP_EOL; // same as -1 echo getKey(1,-15) . PHP_EOL; // same as -1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T3fUr
function name:  (null)
number of ops:  37
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   INIT_FCALL                                               'getkey'
          1        SEND_VAL                                                 1
          2        SEND_VAL                                                 -1
          3        DO_FCALL                                      0  $0      
          4        CONCAT                                           ~1      $0, '%0A'
          5        ECHO                                                     ~1
   26     6        INIT_FCALL                                               'getkey'
          7        SEND_VAL                                                 3
          8        SEND_VAL                                                 1
          9        DO_FCALL                                      0  $2      
         10        CONCAT                                           ~3      $2, '%0A'
         11        ECHO                                                     ~3
   27    12        INIT_FCALL                                               'getkey'
         13        SEND_VAL                                                 7
         14        SEND_VAL                                                 1
         15        DO_FCALL                                      0  $4      
         16        CONCAT                                           ~5      $4, '%0A'
         17        ECHO                                                     ~5
   28    18        INIT_FCALL                                               'getkey'
         19        SEND_VAL                                                 7
         20        SEND_VAL                                                 15
         21        DO_FCALL                                      0  $6      
         22        CONCAT                                           ~7      $6, '%0A'
         23        ECHO                                                     ~7
   29    24        INIT_FCALL                                               'getkey'
         25        SEND_VAL                                                 1
         26        SEND_VAL                                                 -8
         27        DO_FCALL                                      0  $8      
         28        CONCAT                                           ~9      $8, '%0A'
         29        ECHO                                                     ~9
   30    30        INIT_FCALL                                               'getkey'
         31        SEND_VAL                                                 1
         32        SEND_VAL                                                 -15
         33        DO_FCALL                                      0  $10     
         34        CONCAT                                           ~11     $10, '%0A'
         35        ECHO                                                     ~11
         36      > RETURN                                                   1

Function getkey:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T3fUr
function name:  getKey
number of ops:  21
compiled vars:  !0 = $current, !1 = $step, !2 = $keys, !3 = $currentKey, !4 = $size, !5 = $newKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        ASSIGN                                                   !2, <array>
    6     3        INIT_FCALL                                               'array_search'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !2
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !3, $7
    7     8        COUNT                                            ~9      !2
          9        ASSIGN                                                   !4, ~9
   10    10        MOD                                              ~11     !1, !4
         11        ADD                                              ~12     ~11, !4
         12        ASSIGN                                                   !1, ~12
   16    13        ADD                                              ~14     !3, !1
         14        MOD                                              ~15     ~14, !4
         15        ASSIGN                                                   !5, ~15
   20    16        FETCH_DIM_R                                      ~17     !2, !5
         17        VERIFY_RETURN_TYPE                                       ~17
         18      > RETURN                                                   ~17
   21    19*       VERIFY_RETURN_TYPE                                       
         20*     > RETURN                                                   null

End of function getkey

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
129.15 ms | 1407 KiB | 21 Q