3v4l.org

run code in 300+ PHP versions simultaneously
<?php function romanToDecimal($num) { $dec = 0; $i = 0; $romans = ['M', 'D', 'C', 'L', 'X', 'V', 'I']; $decs = [1000, 500, 100, 50, 10, 5, 1]; $rptr = 0; $num .= ' '; while ($i < strlen($num) - 1) { if ($num[$i] === $romans[$rptr]) { if (($rptr > 0) && ($num[$i + 1] === $romans[$rptr - 1])) { $dec -= $decs[$rptr]; $rptr -= 1; } else if (($rptr > 1) && ($num[$i + 1] === $romans[$rptr - 2])) { $dec -= $decs[$rptr]; $rptr -= 2; } else { $dec += $decs[$rptr]; } $i++; } else { $rptr++; if($rptr > count($decs)) return -1; } } return $dec; } function decimal2roman($num) { $romans = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']; $decs = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; $len = count($decs); $rom = ''; while ($num) { $i = 0; while ($decs[$i++] > $num && $i < $len); $rom .= $romans[$i]; $num -= $decs[i]; } return $rom; } echo romanToDecimal('MCXII') . "\n"; echo romanToDecimal('LIII') . "\n"; echo romanToDecimal('XCIX') . "\n"; echo romanToDecimal('XLVII') . "\n"; echo romanToDecimal('I') . "\n"; echo romanToDecimal('IV') . "\n"; echo romanToDecimal('DCCCXCIX') . "\n"; echo romanToDecimal('MMMCMXCIX') . "\n"; echo decimal2roman(128) . "\n"; ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XJsCM
function name:  (null)
number of ops:  46
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   INIT_FCALL                                               'romantodecimal'
          1        SEND_VAL                                                 'MCXII'
          2        DO_FCALL                                      0  $0      
          3        CONCAT                                           ~1      $0, '%0A'
          4        ECHO                                                     ~1
   52     5        INIT_FCALL                                               'romantodecimal'
          6        SEND_VAL                                                 'LIII'
          7        DO_FCALL                                      0  $2      
          8        CONCAT                                           ~3      $2, '%0A'
          9        ECHO                                                     ~3
   53    10        INIT_FCALL                                               'romantodecimal'
         11        SEND_VAL                                                 'XCIX'
         12        DO_FCALL                                      0  $4      
         13        CONCAT                                           ~5      $4, '%0A'
         14        ECHO                                                     ~5
   54    15        INIT_FCALL                                               'romantodecimal'
         16        SEND_VAL                                                 'XLVII'
         17        DO_FCALL                                      0  $6      
         18        CONCAT                                           ~7      $6, '%0A'
         19        ECHO                                                     ~7
   55    20        INIT_FCALL                                               'romantodecimal'
         21        SEND_VAL                                                 'I'
         22        DO_FCALL                                      0  $8      
         23        CONCAT                                           ~9      $8, '%0A'
         24        ECHO                                                     ~9
   56    25        INIT_FCALL                                               'romantodecimal'
         26        SEND_VAL                                                 'IV'
         27        DO_FCALL                                      0  $10     
         28        CONCAT                                           ~11     $10, '%0A'
         29        ECHO                                                     ~11
   57    30        INIT_FCALL                                               'romantodecimal'
         31        SEND_VAL                                                 'DCCCXCIX'
         32        DO_FCALL                                      0  $12     
         33        CONCAT                                           ~13     $12, '%0A'
         34        ECHO                                                     ~13
   58    35        INIT_FCALL                                               'romantodecimal'
         36        SEND_VAL                                                 'MMMCMXCIX'
         37        DO_FCALL                                      0  $14     
         38        CONCAT                                           ~15     $14, '%0A'
         39        ECHO                                                     ~15
   59    40        INIT_FCALL                                               'decimal2roman'
         41        SEND_VAL                                                 128
         42        DO_FCALL                                      0  $16     
         43        CONCAT                                           ~17     $16, '%0A'
         44        ECHO                                                     ~17
   60    45      > RETURN                                                   1

Function romantodecimal:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 51, Position 2 = 8
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 42
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 25
2 jumps found. (Code = 46) Position 1 = 27, Position 2 = 33
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 38
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 33
Branch analysis from position: 20
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 47
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
filename:       /in/XJsCM
function name:  romanToDecimal
number of ops:  53
compiled vars:  !0 = $num, !1 = $dec, !2 = $i, !3 = $romans, !4 = $decs, !5 = $rptr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, 0
    6     2        ASSIGN                                                   !2, 0
    7     3        ASSIGN                                                   !3, <array>
    8     4        ASSIGN                                                   !4, <array>
    9     5        ASSIGN                                                   !5, 0
   11     6        ASSIGN_OP                                     8          !0, '+'
   12     7      > JMP                                                      ->47
   13     8    >   FETCH_DIM_R                                      ~12     !0, !2
          9        FETCH_DIM_R                                      ~13     !3, !5
         10        IS_IDENTICAL                                             ~12, ~13
         11      > JMPZ                                                     ~14, ->42
   14    12    >   IS_SMALLER                                       ~15     0, !5
         13      > JMPZ_EX                                          ~15     ~15, ->20
         14    >   ADD                                              ~16     !2, 1
         15        FETCH_DIM_R                                      ~17     !0, ~16
         16        SUB                                              ~18     !5, 1
         17        FETCH_DIM_R                                      ~19     !3, ~18
         18        IS_IDENTICAL                                     ~20     ~17, ~19
         19        BOOL                                             ~15     ~20
         20    > > JMPZ                                                     ~15, ->25
   15    21    >   FETCH_DIM_R                                      ~21     !4, !5
         22        ASSIGN_OP                                     2          !1, ~21
   16    23        ASSIGN_OP                                     2          !5, 1
         24      > JMP                                                      ->40
   17    25    >   IS_SMALLER                                       ~24     1, !5
         26      > JMPZ_EX                                          ~24     ~24, ->33
         27    >   ADD                                              ~25     !2, 1
         28        FETCH_DIM_R                                      ~26     !0, ~25
         29        SUB                                              ~27     !5, 2
         30        FETCH_DIM_R                                      ~28     !3, ~27
         31        IS_IDENTICAL                                     ~29     ~26, ~28
         32        BOOL                                             ~24     ~29
         33    > > JMPZ                                                     ~24, ->38
   18    34    >   FETCH_DIM_R                                      ~30     !4, !5
         35        ASSIGN_OP                                     2          !1, ~30
   19    36        ASSIGN_OP                                     2          !5, 2
         37      > JMP                                                      ->40
   21    38    >   FETCH_DIM_R                                      ~33     !4, !5
         39        ASSIGN_OP                                     1          !1, ~33
   23    40    >   PRE_INC                                                  !2
         41      > JMP                                                      ->47
   25    42    >   PRE_INC                                                  !5
   26    43        COUNT                                            ~37     !4
         44        IS_SMALLER                                               ~37, !5
         45      > JMPZ                                                     ~38, ->47
   27    46    > > RETURN                                                   -1
   12    47    >   STRLEN                                           ~39     !0
         48        SUB                                              ~40     ~39, 1
         49        IS_SMALLER                                               !2, ~40
         50      > JMPNZ                                                    ~41, ->8
   31    51    > > RETURN                                                   !1
   32    52*     > RETURN                                                   null

End of function romantodecimal

Function decimal2roman:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 7
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 9
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 7
Branch analysis from position: 22
Branch analysis from position: 7
Branch analysis from position: 9
Branch analysis from position: 15
filename:       /in/XJsCM
function name:  decimal2roman
number of ops:  24
compiled vars:  !0 = $num, !1 = $romans, !2 = $decs, !3 = $len, !4 = $rom, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   36     1        ASSIGN                                                   !1, <array>
   37     2        ASSIGN                                                   !2, <array>
   38     3        COUNT                                            ~8      !2
          4        ASSIGN                                                   !3, ~8
   39     5        ASSIGN                                                   !4, ''
   41     6      > JMP                                                      ->21
   42     7    >   ASSIGN                                                   !5, 0
   43     8      > JMP                                                      ->9
          9    >   POST_INC                                         ~12     !5
         10        FETCH_DIM_R                                      ~13     !2, ~12
         11        IS_SMALLER                                       ~14     !0, ~13
         12      > JMPZ_EX                                          ~14     ~14, ->15
         13    >   IS_SMALLER                                       ~15     !5, !3
         14        BOOL                                             ~14     ~15
         15    > > JMPNZ                                                    ~14, ->9
   44    16    >   FETCH_DIM_R                                      ~16     !1, !5
         17        ASSIGN_OP                                     8          !4, ~16
   45    18        FETCH_CONSTANT                                   ~18     'i'
         19        FETCH_DIM_R                                      ~19     !2, ~18
         20        ASSIGN_OP                                     2          !0, ~19
   41    21    > > JMPNZ                                                    !0, ->7
   48    22    > > RETURN                                                   !4
   49    23*     > RETURN                                                   null

End of function decimal2roman

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.86 ms | 1411 KiB | 22 Q