3v4l.org

run code in 300+ PHP versions simultaneously
<?php function nth_position($str, $letter, $n, $offset = 0){ $str_arr = str_split($str); $letter_size = array_count_values(str_split(substr($str, $offset))); if( !isset($letter_size[$letter])){ trigger_error('letter "' . $letter . '" does not exist in ' . $str . ' after ' . $offset . '. position', E_USER_WARNING); return false; } else if($letter_size[$letter] < $n) { trigger_error('letter "' . $letter . '" does not exist ' . $n .' times in ' . $str . ' after ' . $offset . '. position', E_USER_WARNING); return false; } for($i = $offset, $x = 0, $count = (count($str_arr) - $offset); $i < $count, $x != $n; $i++){ if($str_arr[$i] == $letter){ $x++; } } return $i - 1; } echo nth_position('foobarbaz', 'a', 2); //7 echo nth_position('foobarbaz', 'b', 1, 4); //6
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UUSa6
function name:  (null)
number of ops:  14
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   INIT_FCALL                                               'nth_position'
          1        SEND_VAL                                                 'foobarbaz'
          2        SEND_VAL                                                 'a'
          3        SEND_VAL                                                 2
          4        DO_FCALL                                      0  $0      
          5        ECHO                                                     $0
   22     6        INIT_FCALL                                               'nth_position'
          7        SEND_VAL                                                 'foobarbaz'
          8        SEND_VAL                                                 'b'
          9        SEND_VAL                                                 1
         10        SEND_VAL                                                 4
         11        DO_FCALL                                      0  $1      
         12        ECHO                                                     $1
         13      > RETURN                                                   1

Function nth_position:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 34
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 50
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 56
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 60
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 56
Branch analysis from position: 65
Branch analysis from position: 56
Branch analysis from position: 60
filename:       /in/UUSa6
function name:  nth_position
number of ops:  68
compiled vars:  !0 = $str, !1 = $letter, !2 = $n, !3 = $offset, !4 = $str_arr, !5 = $letter_size, !6 = $i, !7 = $x, !8 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      0
    4     4        INIT_FCALL                                               'str_split'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $9      
          7        ASSIGN                                                   !4, $9
    5     8        INIT_FCALL                                               'array_count_values'
          9        INIT_FCALL                                               'str_split'
         10        INIT_FCALL                                               'substr'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !3
         13        DO_ICALL                                         $11     
         14        SEND_VAR                                                 $11
         15        DO_ICALL                                         $12     
         16        SEND_VAR                                                 $12
         17        DO_ICALL                                         $13     
         18        ASSIGN                                                   !5, $13
    6    19        ISSET_ISEMPTY_DIM_OBJ                         0  ~15     !5, !1
         20        BOOL_NOT                                         ~16     ~15
         21      > JMPZ                                                     ~16, ->34
    7    22    >   INIT_FCALL                                               'trigger_error'
         23        CONCAT                                           ~17     'letter+%22', !1
         24        CONCAT                                           ~18     ~17, '%22+does+not+exist+in+'
         25        CONCAT                                           ~19     ~18, !0
         26        CONCAT                                           ~20     ~19, '+after+'
         27        CONCAT                                           ~21     ~20, !3
         28        CONCAT                                           ~22     ~21, '.+position'
         29        SEND_VAL                                                 ~22
         30        SEND_VAL                                                 512
         31        DO_ICALL                                                 
    8    32      > RETURN                                                   <false>
         33*       JMP                                                      ->50
    9    34    >   FETCH_DIM_R                                      ~24     !5, !1
         35        IS_SMALLER                                               ~24, !2
         36      > JMPZ                                                     ~25, ->50
   10    37    >   INIT_FCALL                                               'trigger_error'
         38        CONCAT                                           ~26     'letter+%22', !1
         39        CONCAT                                           ~27     ~26, '%22+does+not+exist+'
         40        CONCAT                                           ~28     ~27, !2
         41        CONCAT                                           ~29     ~28, '+times+in+'
         42        CONCAT                                           ~30     ~29, !0
         43        CONCAT                                           ~31     ~30, '+after+'
         44        CONCAT                                           ~32     ~31, !3
         45        CONCAT                                           ~33     ~32, '.+position'
         46        SEND_VAL                                                 ~33
         47        SEND_VAL                                                 512
         48        DO_ICALL                                                 
   11    49      > RETURN                                                   <false>
   13    50    >   ASSIGN                                                   !6, !3
         51        ASSIGN                                                   !7, 0
         52        COUNT                                            ~37     !4
         53        SUB                                              ~38     ~37, !3
         54        ASSIGN                                                   !8, ~38
         55      > JMP                                                      ->61
   14    56    >   FETCH_DIM_R                                      ~40     !4, !6
         57        IS_EQUAL                                                 !1, ~40
         58      > JMPZ                                                     ~41, ->60
   15    59    >   PRE_INC                                                  !7
   13    60    >   PRE_INC                                                  !6
         61    >   IS_SMALLER                                       ~44     !6, !8
         62        FREE                                                     ~44
         63        IS_NOT_EQUAL                                             !7, !2
         64      > JMPNZ                                                    ~45, ->56
   18    65    >   SUB                                              ~46     !6, 1
         66      > RETURN                                                   ~46
   19    67*     > RETURN                                                   null

End of function nth_position

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
281.95 ms | 1411 KiB | 24 Q