3v4l.org

run code in 300+ PHP versions simultaneously
<?php $number = 1000000; echo convert_number_to_words($number); function convert_number_to_words($number) { $hyphen = '-'; $conjunction = ' and '; $separator = ', '; $negative = 'negative '; $decimal = ' point '; $dictionary = array( 0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten', 11 => 'eleven', 12 => 'twelve', 13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen', 16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen', 19 => 'nineteen', 20 => 'twenty', 30 => 'thirty', 40 => 'fourty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety', 100 => 'hundred', 1000 => 'thousand', 100000 => 'lakhs', 10000000 => 'cores', 1000000000 => 'billion', 1000000000000 => 'trillion', 1000000000000000 => 'quadrillion', 1000000000000000000 => 'quintillion' ); if (!is_numeric($number)) { return false; } if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) { // overflow trigger_error( 'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX, E_USER_WARNING ); return false; } if ($number < 0) { return $negative . convert_number_to_words(abs($number)); } $string = $fraction = null; if (strpos($number, '.') !== false) { list($number, $fraction) = explode('.', $number); } switch (true) { case $number < 21: $string = $dictionary[$number]; break; case $number < 100: $tens = ((int) ($number / 10)) * 10; $units = $number % 10; $string = $dictionary[$tens]; if ($units) { $string .= $hyphen . $dictionary[$units]; } break; case $number < 1000: $hundreds = $number / 100; $remainder = $number % 100; $string = $dictionary[$hundreds] . ' ' . $dictionary[100]; if ($remainder) { $string .= $conjunction . convert_number_to_words($remainder); } break; default: $pow = floor(log($number, 10)); $remainer_val = 100; if($pow >= 3 && $pow < 5 ) { $baseUnit = pow(1000, floor(log($number, 1000))); } else if($pow >= 5 && $pow < 7 ) { $baseUnit = 100000; $remainer_val = 10; } else if($pow >= 7 && $pow < 9 ) { $baseUnit = 10000000; $remainer_val = 10; } else { $baseUnit = pow(1000, floor(log($number, 1000))); } $numBaseUnits = (int) ($number / $baseUnit); $remainder = $number % $baseUnit; $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit]; if ($remainder) { $string .= $remainder < $remainer_val ? $conjunction : $separator; $string .= convert_number_to_words($remainder); } break; } if (null !== $fraction && is_numeric($fraction)) { $string .= $decimal; $words = array(); foreach (str_split((string) $fraction) as $number) { $words[] = $dictionary[$number]; } $string .= implode(' ', $words); } echo $string; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OoIZ0
function name:  (null)
number of ops:  6
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, 1000000
    3     1        INIT_FCALL_BY_NAME                                       'convert_number_to_words'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4        ECHO                                                     $2
  127     5      > RETURN                                                   1

Function convert_number_to_words:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 46) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
2 jumps found. (Code = 47) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 28
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 38
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 55
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 57, Position 2 = 62
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 65
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 78
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 94
Branch analysis from position: 94
2 jumps found. (Code = 46) Position 1 = 105, Position 2 = 107
Branch analysis from position: 105
2 jumps found. (Code = 43) Position 1 = 108, Position 2 = 121
Branch analysis from position: 108
1 jumps found. (Code = 42) Position 1 = 149
Branch analysis from position: 149
2 jumps found. (Code = 43) Position 1 = 162, Position 2 = 172
Branch analysis from position: 162
2 jumps found. (Code = 43) Position 1 = 164, Position 2 = 166
Branch analysis from position: 164
1 jumps found. (Code = 42) Position 1 = 167
Branch analysis from position: 167
1 jumps found. (Code = 42) Position 1 = 173
Branch analysis from position: 173
2 jumps found. (Code = 46) Position 1 = 175, Position 2 = 179
Branch analysis from position: 175
2 jumps found. (Code = 43) Position 1 = 180, Position 2 = 198
Branch analysis from position: 180
2 jumps found. (Code = 77) Position 1 = 187, Position 2 = 192
Branch analysis from position: 187
2 jumps found. (Code = 78) Position 1 = 188, Position 2 = 192
Branch analysis from position: 188
1 jumps found. (Code = 42) Position 1 = 187
Branch analysis from position: 187
Branch analysis from position: 192
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 192
Branch analysis from position: 198
Branch analysis from position: 179
Branch analysis from position: 166
1 jumps found. (Code = 42) Position 1 = 173
Branch analysis from position: 173
Branch analysis from position: 172
Branch analysis from position: 121
2 jumps found. (Code = 46) Position 1 = 123, Position 2 = 125
Branch analysis from position: 123
2 jumps found. (Code = 43) Position 1 = 126, Position 2 = 129
Branch analysis from position: 126
1 jumps found. (Code = 42) Position 1 = 149
Branch analysis from position: 149
Branch analysis from position: 129
2 jumps found. (Code = 46) Position 1 = 131, Position 2 = 133
Branch analysis from position: 131
2 jumps found. (Code = 43) Position 1 = 134, Position 2 = 137
Branch analysis from position: 134
1 jumps found. (Code = 42) Position 1 = 149
Branch analysis from position: 149
Branch analysis from position: 137
2 jumps found. (Code = 43) Position 1 = 162, Position 2 = 172
Branch analysis from position: 162
Branch analysis from position: 172
Branch analysis from position: 133
Branch analysis from position: 125
Branch analysis from position: 107
Branch analysis from position: 78
2 jumps found. (Code = 43) Position 1 = 88, Position 2 = 93
Branch analysis from position: 88
1 jumps found. (Code = 42) Position 1 = 173
Branch analysis from position: 173
Branch analysis from position: 93
Branch analysis from position: 65
2 jumps found. (Code = 43) Position 1 = 74, Position 2 = 77
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 173
Branch analysis from position: 173
Branch analysis from position: 77
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 173
Branch analysis from position: 173
Branch analysis from position: 55
Branch analysis from position: 22
Branch analysis from position: 18
filename:       /in/OoIZ0
function name:  convert_number_to_words
number of ops:  200
compiled vars:  !0 = $number, !1 = $hyphen, !2 = $conjunction, !3 = $separator, !4 = $negative, !5 = $decimal, !6 = $dictionary, !7 = $string, !8 = $fraction, !9 = $tens, !10 = $units, !11 = $hundreds, !12 = $remainder, !13 = $pow, !14 = $remainer_val, !15 = $baseUnit, !16 = $numBaseUnits, !17 = $words
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    6     1        ASSIGN                                                   !1, '-'
    7     2        ASSIGN                                                   !2, '+and+'
    8     3        ASSIGN                                                   !3, '%2C+'
    9     4        ASSIGN                                                   !4, 'negative+'
   10     5        ASSIGN                                                   !5, '+point+'
   11     6        ASSIGN                                                   !6, <array>
   50     7        INIT_FCALL                                               'is_numeric'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $24     
         10        BOOL_NOT                                         ~25     $24
         11      > JMPZ                                                     ~25, ->13
   51    12    > > RETURN                                                   <false>
   54    13    >   IS_SMALLER_OR_EQUAL                              ~26     0, !0
         14      > JMPZ_EX                                          ~26     ~26, ->18
         15    >   CAST                                          4  ~27     !0
         16        IS_SMALLER                                       ~28     ~27, 0
         17        BOOL                                             ~26     ~28
         18    > > JMPNZ_EX                                         ~26     ~26, ->22
         19    >   CAST                                          4  ~29     !0
         20        IS_SMALLER                                       ~30     ~29, -9223372036854775807
         21        BOOL                                             ~26     ~30
         22    > > JMPZ                                                     ~26, ->28
   56    23    >   INIT_FCALL                                               'trigger_error'
   57    24        SEND_VAL                                                 'convert_number_to_words+only+accepts+numbers+between+-9223372036854775807+and+9223372036854775807'
   58    25        SEND_VAL                                                 512
         26        DO_ICALL                                                 
   60    27      > RETURN                                                   <false>
   63    28    >   IS_SMALLER                                               !0, 0
         29      > JMPZ                                                     ~32, ->38
   64    30    >   INIT_FCALL_BY_NAME                                       'convert_number_to_words'
         31        INIT_FCALL                                               'abs'
         32        SEND_VAR                                                 !0
         33        DO_ICALL                                         $33     
         34        SEND_VAR_NO_REF_EX                                       $33
         35        DO_FCALL                                      0  $34     
         36        CONCAT                                           ~35     !4, $34
         37      > RETURN                                                   ~35
   67    38    >   ASSIGN                                           ~36     !8, null
         39        ASSIGN                                                   !7, ~36
   69    40        INIT_FCALL                                               'strpos'
         41        SEND_VAR                                                 !0
         42        SEND_VAL                                                 '.'
         43        DO_ICALL                                         $38     
         44        TYPE_CHECK                                  1018          $38
         45      > JMPZ                                                     ~39, ->55
   70    46    >   INIT_FCALL                                               'explode'
         47        SEND_VAL                                                 '.'
         48        SEND_VAR                                                 !0
         49        DO_ICALL                                         $40     
         50        FETCH_LIST_R                                     $41     $40, 0
         51        ASSIGN                                                   !0, $41
         52        FETCH_LIST_R                                     $43     $40, 1
         53        ASSIGN                                                   !8, $43
         54        FREE                                                     $40
   74    55    >   IS_SMALLER                                               !0, 21
         56      > JMPNZ                                                    ~46, ->62
   77    57    >   IS_SMALLER                                               !0, 100
         58      > JMPNZ                                                    ~47, ->65
   85    59    >   IS_SMALLER                                               !0, 1000
         60      > JMPNZ                                                    ~48, ->78
         61    > > JMP                                                      ->94
   75    62    >   FETCH_DIM_R                                      ~49     !6, !0
         63        ASSIGN                                                   !7, ~49
   76    64      > JMP                                                      ->173
   78    65    >   DIV                                              ~51     !0, 10
         66        CAST                                          4  ~52     ~51
         67        MUL                                              ~53     ~52, 10
         68        ASSIGN                                                   !9, ~53
   79    69        MOD                                              ~55     !0, 10
         70        ASSIGN                                                   !10, ~55
   80    71        FETCH_DIM_R                                      ~57     !6, !9
         72        ASSIGN                                                   !7, ~57
   81    73      > JMPZ                                                     !10, ->77
   82    74    >   FETCH_DIM_R                                      ~59     !6, !10
         75        CONCAT                                           ~60     !1, ~59
         76        ASSIGN_OP                                     8          !7, ~60
   84    77    > > JMP                                                      ->173
   86    78    >   DIV                                              ~62     !0, 100
         79        ASSIGN                                                   !11, ~62
   87    80        MOD                                              ~64     !0, 100
         81        ASSIGN                                                   !12, ~64
   88    82        FETCH_DIM_R                                      ~66     !6, !11
         83        CONCAT                                           ~67     ~66, '+'
         84        FETCH_DIM_R                                      ~68     !6, 100
         85        CONCAT                                           ~69     ~67, ~68
         86        ASSIGN                                                   !7, ~69
   89    87      > JMPZ                                                     !12, ->93
   90    88    >   INIT_FCALL_BY_NAME                                       'convert_number_to_words'
         89        SEND_VAR_EX                                              !12
         90        DO_FCALL                                      0  $71     
         91        CONCAT                                           ~72     !2, $71
         92        ASSIGN_OP                                     8          !7, ~72
   92    93    > > JMP                                                      ->173
   94    94    >   INIT_FCALL                                               'floor'
         95        INIT_FCALL                                               'log'
         96        SEND_VAR                                                 !0
         97        SEND_VAL                                                 10
         98        DO_ICALL                                         $74     
         99        SEND_VAR                                                 $74
        100        DO_ICALL                                         $75     
        101        ASSIGN                                                   !13, $75
   95   102        ASSIGN                                                   !14, 100
   96   103        IS_SMALLER_OR_EQUAL                              ~78     3, !13
        104      > JMPZ_EX                                          ~78     ~78, ->107
        105    >   IS_SMALLER                                       ~79     !13, 5
        106        BOOL                                             ~78     ~79
        107    > > JMPZ                                                     ~78, ->121
   97   108    >   INIT_FCALL                                               'pow'
        109        SEND_VAL                                                 1000
        110        INIT_FCALL                                               'floor'
        111        INIT_FCALL                                               'log'
        112        SEND_VAR                                                 !0
        113        SEND_VAL                                                 1000
        114        DO_ICALL                                         $80     
        115        SEND_VAR                                                 $80
        116        DO_ICALL                                         $81     
        117        SEND_VAR                                                 $81
        118        DO_ICALL                                         $82     
        119        ASSIGN                                                   !15, $82
        120      > JMP                                                      ->149
   98   121    >   IS_SMALLER_OR_EQUAL                              ~84     5, !13
        122      > JMPZ_EX                                          ~84     ~84, ->125
        123    >   IS_SMALLER                                       ~85     !13, 7
        124        BOOL                                             ~84     ~85
        125    > > JMPZ                                                     ~84, ->129
   99   126    >   ASSIGN                                                   !15, 100000
  100   127        ASSIGN                                                   !14, 10
        128      > JMP                                                      ->149
  101   129    >   IS_SMALLER_OR_EQUAL                              ~88     7, !13
        130      > JMPZ_EX                                          ~88     ~88, ->133
        131    >   IS_SMALLER                                       ~89     !13, 9
        132        BOOL                                             ~88     ~89
        133    > > JMPZ                                                     ~88, ->137
  102   134    >   ASSIGN                                                   !15, 10000000
  103   135        ASSIGN                                                   !14, 10
        136      > JMP                                                      ->149
  105   137    >   INIT_FCALL                                               'pow'
        138        SEND_VAL                                                 1000
        139        INIT_FCALL                                               'floor'
        140        INIT_FCALL                                               'log'
        141        SEND_VAR                                                 !0
        142        SEND_VAL                                                 1000
        143        DO_ICALL                                         $92     
        144        SEND_VAR                                                 $92
        145        DO_ICALL                                         $93     
        146        SEND_VAR                                                 $93
        147        DO_ICALL                                         $94     
        148        ASSIGN                                                   !15, $94
  108   149    >   DIV                                              ~96     !0, !15
        150        CAST                                          4  ~97     ~96
        151        ASSIGN                                                   !16, ~97
  109   152        MOD                                              ~99     !0, !15
        153        ASSIGN                                                   !12, ~99
  110   154        INIT_FCALL_BY_NAME                                       'convert_number_to_words'
        155        SEND_VAR_EX                                              !16
        156        DO_FCALL                                      0  $101    
        157        CONCAT                                           ~102    $101, '+'
        158        FETCH_DIM_R                                      ~103    !6, !15
        159        CONCAT                                           ~104    ~102, ~103
        160        ASSIGN                                                   !7, ~104
  111   161      > JMPZ                                                     !12, ->172
  112   162    >   IS_SMALLER                                               !12, !14
        163      > JMPZ                                                     ~106, ->166
        164    >   QM_ASSIGN                                        ~107    !2
        165      > JMP                                                      ->167
        166    >   QM_ASSIGN                                        ~107    !3
        167    >   ASSIGN_OP                                     8          !7, ~107
  113   168        INIT_FCALL_BY_NAME                                       'convert_number_to_words'
        169        SEND_VAR_EX                                              !12
        170        DO_FCALL                                      0  $109    
        171        ASSIGN_OP                                     8          !7, $109
  115   172    > > JMP                                                      ->173
  118   173    >   TYPE_CHECK                                  1020  ~111    !8
        174      > JMPZ_EX                                          ~111    ~111, ->179
        175    >   INIT_FCALL                                               'is_numeric'
        176        SEND_VAR                                                 !8
        177        DO_ICALL                                         $112    
        178        BOOL                                             ~111    $112
        179    > > JMPZ                                                     ~111, ->198
  119   180    >   ASSIGN_OP                                     8          !7, !5
  120   181        ASSIGN                                                   !17, <array>
  121   182        INIT_FCALL                                               'str_split'
        183        CAST                                          6  ~115    !8
        184        SEND_VAL                                                 ~115
        185        DO_ICALL                                         $116    
        186      > FE_RESET_R                                       $117    $116, ->192
        187    > > FE_FETCH_R                                               $117, !0, ->192
  122   188    >   FETCH_DIM_R                                      ~119    !6, !0
        189        ASSIGN_DIM                                               !17
        190        OP_DATA                                                  ~119
  121   191      > JMP                                                      ->187
        192    >   FE_FREE                                                  $117
  124   193        INIT_FCALL                                               'implode'
        194        SEND_VAL                                                 '+'
        195        SEND_VAR                                                 !17
        196        DO_ICALL                                         $120    
        197        ASSIGN_OP                                     8          !7, $120
  126   198    >   ECHO                                                     !7
  127   199      > RETURN                                                   null

End of function convert_number_to_words

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.53 ms | 1416 KiB | 33 Q