3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = "we test coders"; echo reverse_word($str) . "\n"; function reverse_word($input) { $num_ws = 0; $p = 0; while(strpos($input, " ", $p) !== false) { $num_ws ++; $p = strpos($input, ' ', $p) + 1; } //echo "num ws is $num_ws\n"; $p = 0; for($i=0; $i<$num_ws + 1; $i++) { $ws_index = strpos($input, " ", $p); if($ws_index === false) { $word = substr($input, $p); } else { $length = $ws_index - $p; $word = substr($input, $p, $length); } $result[] = $word; $p = $ws_index + 1; //move onto first char of next word } print_r($result); $str = ''; for($i=0; $i<count($result); $i++) { $str .= reverse($result[$i]) . " "; } //return $str; } function reverse($str) { $a = 0; $b = strlen($str)-1; while($a < $b) { swap($str, $a, $b); $a ++; $b --; } return $str; } function swap(&$str, $i1, $i2) { $tmp = $str[$i1]; $str[$i1] = $str[$i2]; $str[$i2] = $tmp; } ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qRgK5
function name:  (null)
number of ops:  7
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'we+test+coders'
    5     1        INIT_FCALL_BY_NAME                                       'reverse_word'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4        CONCAT                                           ~3      $2, '%0A'
          5        ECHO                                                     ~3
   56     6      > RETURN                                                   1

Function reverse_word:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 4
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 22
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 58
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 58
Branch analysis from position: 69
Branch analysis from position: 58
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 36
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 22
Branch analysis from position: 52
Branch analysis from position: 22
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 22
Branch analysis from position: 52
Branch analysis from position: 22
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 4
Branch analysis from position: 19
Branch analysis from position: 4
filename:       /in/qRgK5
function name:  reverse_word
number of ops:  70
compiled vars:  !0 = $input, !1 = $num_ws, !2 = $p, !3 = $i, !4 = $ws_index, !5 = $word, !6 = $length, !7 = $result, !8 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    8     1        ASSIGN                                                   !1, 0
    9     2        ASSIGN                                                   !2, 0
   10     3      > JMP                                                      ->12
   11     4    >   PRE_INC                                                  !1
   12     5        INIT_FCALL                                               'strpos'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 '+'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $12     
         10        ADD                                              ~13     $12, 1
         11        ASSIGN                                                   !2, ~13
   10    12    >   INIT_FCALL                                               'strpos'
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 '+'
         15        SEND_VAR                                                 !2
         16        DO_ICALL                                         $15     
         17        TYPE_CHECK                                  1018          $15
         18      > JMPNZ                                                    ~16, ->4
   17    19    >   ASSIGN                                                   !2, 0
   18    20        ASSIGN                                                   !3, 0
         21      > JMP                                                      ->49
   19    22    >   INIT_FCALL                                               'strpos'
         23        SEND_VAR                                                 !0
         24        SEND_VAL                                                 '+'
         25        SEND_VAR                                                 !2
         26        DO_ICALL                                         $19     
         27        ASSIGN                                                   !4, $19
   20    28        TYPE_CHECK                                    4          !4
         29      > JMPZ                                                     ~21, ->36
   21    30    >   INIT_FCALL                                               'substr'
         31        SEND_VAR                                                 !0
         32        SEND_VAR                                                 !2
         33        DO_ICALL                                         $22     
         34        ASSIGN                                                   !5, $22
         35      > JMP                                                      ->44
   24    36    >   SUB                                              ~24     !4, !2
         37        ASSIGN                                                   !6, ~24
   25    38        INIT_FCALL                                               'substr'
         39        SEND_VAR                                                 !0
         40        SEND_VAR                                                 !2
         41        SEND_VAR                                                 !6
         42        DO_ICALL                                         $26     
         43        ASSIGN                                                   !5, $26
   27    44    >   ASSIGN_DIM                                               !7
         45        OP_DATA                                                  !5
   28    46        ADD                                              ~29     !4, 1
         47        ASSIGN                                                   !2, ~29
   18    48        PRE_INC                                                  !3
         49    >   ADD                                              ~32     !1, 1
         50        IS_SMALLER                                               !3, ~32
         51      > JMPNZ                                                    ~33, ->22
   31    52    >   INIT_FCALL                                               'print_r'
         53        SEND_VAR                                                 !7
         54        DO_ICALL                                                 
   32    55        ASSIGN                                                   !8, ''
   33    56        ASSIGN                                                   !3, 0
         57      > JMP                                                      ->66
   34    58    >   INIT_FCALL_BY_NAME                                       'reverse'
         59        CHECK_FUNC_ARG                                           
         60        FETCH_DIM_FUNC_ARG                               $37     !7, !3
         61        SEND_FUNC_ARG                                            $37
         62        DO_FCALL                                      0  $38     
         63        CONCAT                                           ~39     $38, '+'
         64        ASSIGN_OP                                     8          !8, ~39
   33    65        PRE_INC                                                  !3
         66    >   COUNT                                            ~42     !7
         67        IS_SMALLER                                               !3, ~42
         68      > JMPNZ                                                    ~43, ->58
   37    69    > > RETURN                                                   null

End of function reverse_word

Function reverse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 6
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 6
Branch analysis from position: 15
Branch analysis from position: 6
filename:       /in/qRgK5
function name:  reverse
number of ops:  17
compiled vars:  !0 = $str, !1 = $a, !2 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   40     1        ASSIGN                                                   !1, 0
   41     2        STRLEN                                           ~4      !0
          3        SUB                                              ~5      ~4, 1
          4        ASSIGN                                                   !2, ~5
   42     5      > JMP                                                      ->13
   43     6    >   INIT_FCALL_BY_NAME                                       'swap'
          7        SEND_VAR_EX                                              !0
          8        SEND_VAR_EX                                              !1
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0          
   44    11        PRE_INC                                                  !1
   45    12        PRE_DEC                                                  !2
   42    13    >   IS_SMALLER                                               !1, !2
         14      > JMPNZ                                                    ~10, ->6
   47    15    > > RETURN                                                   !0
   48    16*     > RETURN                                                   null

End of function reverse

Function swap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qRgK5
function name:  swap
number of ops:  11
compiled vars:  !0 = $str, !1 = $i1, !2 = $i2, !3 = $tmp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   51     3        FETCH_DIM_R                                      ~4      !0, !1
          4        ASSIGN                                                   !3, ~4
   52     5        FETCH_DIM_R                                      ~7      !0, !2
          6        ASSIGN_DIM                                               !0, !1
          7        OP_DATA                                                  ~7
   53     8        ASSIGN_DIM                                               !0, !2
          9        OP_DATA                                                  !3
   54    10      > RETURN                                                   null

End of function swap

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.86 ms | 1404 KiB | 19 Q