3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = '1234567'; $array = ['1', '12', '123', '1234', '456', '56', '7', '3456', '234567']; function get_best_prefix($input, $array){ $best = ''; $sizeBest = 0; foreach ($array as $data) { if (strpos($input, $data) === 0) { $current = strlen($data); if($sizeBest < $current){ $sizeBest = $current; $best = $data; } } } return $best; } echo get_best_prefix($input, $array);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XjZ9Q
function name:  (null)
number of ops:  8
compiled vars:  !0 = $input, !1 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '1234567'
    4     1        ASSIGN                                                   !1, <array>
   28     2        INIT_FCALL                                               'get_best_prefix'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
          7      > RETURN                                                   1

Function get_best_prefix:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 19
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 19
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 18
Branch analysis from position: 18
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/XjZ9Q
function name:  get_best_prefix
number of ops:  22
compiled vars:  !0 = $input, !1 = $array, !2 = $best, !3 = $sizeBest, !4 = $data, !5 = $current
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2        ASSIGN                                                   !2, ''
   10     3        ASSIGN                                                   !3, 0
   12     4      > FE_RESET_R                                       $8      !1, ->19
          5    > > FE_FETCH_R                                               $8, !4, ->19
   13     6    >   INIT_FCALL                                               'strpos'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !4
          9        DO_ICALL                                         $9      
         10        IS_IDENTICAL                                             $9, 0
         11      > JMPZ                                                     ~10, ->18
   14    12    >   STRLEN                                           ~11     !4
         13        ASSIGN                                                   !5, ~11
   15    14        IS_SMALLER                                               !3, !5
         15      > JMPZ                                                     ~13, ->18
   16    16    >   ASSIGN                                                   !3, !5
   17    17        ASSIGN                                                   !2, !4
   12    18    > > JMP                                                      ->5
         19    >   FE_FREE                                                  $8
   24    20      > RETURN                                                   !2
   26    21*     > RETURN                                                   null

End of function get_best_prefix

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.2 ms | 1399 KiB | 16 Q