3v4l.org

run code in 300+ PHP versions simultaneously
<?php 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', 1000000 => 'million', 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: $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 < 100 ? $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); } return $string; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GEkqt
function name:  (null)
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  112     0  E > > 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 = 43) Position 1 = 119, Position 2 = 129
Branch analysis from position: 119
2 jumps found. (Code = 43) Position 1 = 121, Position 2 = 123
Branch analysis from position: 121
1 jumps found. (Code = 42) Position 1 = 124
Branch analysis from position: 124
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
2 jumps found. (Code = 46) Position 1 = 132, Position 2 = 136
Branch analysis from position: 132
2 jumps found. (Code = 43) Position 1 = 137, Position 2 = 155
Branch analysis from position: 137
2 jumps found. (Code = 77) Position 1 = 144, Position 2 = 149
Branch analysis from position: 144
2 jumps found. (Code = 78) Position 1 = 145, Position 2 = 149
Branch analysis from position: 145
1 jumps found. (Code = 42) Position 1 = 144
Branch analysis from position: 144
Branch analysis from position: 149
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 149
Branch analysis from position: 155
Branch analysis from position: 136
Branch analysis from position: 123
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 129
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 = 130
Branch analysis from position: 130
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 = 130
Branch analysis from position: 130
Branch analysis from position: 77
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 55
Branch analysis from position: 22
Branch analysis from position: 18
filename:       /in/GEkqt
function name:  convert_number_to_words
number of ops:  157
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 = $baseUnit, !14 = $numBaseUnits, !15 = $words
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, '-'
    5     2        ASSIGN                                                   !2, '+and+'
    6     3        ASSIGN                                                   !3, '%2C+'
    7     4        ASSIGN                                                   !4, 'negative+'
    8     5        ASSIGN                                                   !5, '+point+'
    9     6        ASSIGN                                                   !6, <array>
   47     7        INIT_FCALL                                               'is_numeric'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $22     
         10        BOOL_NOT                                         ~23     $22
         11      > JMPZ                                                     ~23, ->13
   48    12    > > RETURN                                                   <false>
   51    13    >   IS_SMALLER_OR_EQUAL                              ~24     0, !0
         14      > JMPZ_EX                                          ~24     ~24, ->18
         15    >   CAST                                          4  ~25     !0
         16        IS_SMALLER                                       ~26     ~25, 0
         17        BOOL                                             ~24     ~26
         18    > > JMPNZ_EX                                         ~24     ~24, ->22
         19    >   CAST                                          4  ~27     !0
         20        IS_SMALLER                                       ~28     ~27, -9223372036854775807
         21        BOOL                                             ~24     ~28
         22    > > JMPZ                                                     ~24, ->28
   53    23    >   INIT_FCALL                                               'trigger_error'
   54    24        SEND_VAL                                                 'convert_number_to_words+only+accepts+numbers+between+-9223372036854775807+and+9223372036854775807'
   55    25        SEND_VAL                                                 512
         26        DO_ICALL                                                 
   57    27      > RETURN                                                   <false>
   60    28    >   IS_SMALLER                                               !0, 0
         29      > JMPZ                                                     ~30, ->38
   61    30    >   INIT_FCALL_BY_NAME                                       'convert_number_to_words'
         31        INIT_FCALL                                               'abs'
         32        SEND_VAR                                                 !0
         33        DO_ICALL                                         $31     
         34        SEND_VAR_NO_REF_EX                                       $31
         35        DO_FCALL                                      0  $32     
         36        CONCAT                                           ~33     !4, $32
         37      > RETURN                                                   ~33
   64    38    >   ASSIGN                                           ~34     !8, null
         39        ASSIGN                                                   !7, ~34
   66    40        INIT_FCALL                                               'strpos'
         41        SEND_VAR                                                 !0
         42        SEND_VAL                                                 '.'
         43        DO_ICALL                                         $36     
         44        TYPE_CHECK                                  1018          $36
         45      > JMPZ                                                     ~37, ->55
   67    46    >   INIT_FCALL                                               'explode'
         47        SEND_VAL                                                 '.'
         48        SEND_VAR                                                 !0
         49        DO_ICALL                                         $38     
         50        FETCH_LIST_R                                     $39     $38, 0
         51        ASSIGN                                                   !0, $39
         52        FETCH_LIST_R                                     $41     $38, 1
         53        ASSIGN                                                   !8, $41
         54        FREE                                                     $38
   71    55    >   IS_SMALLER                                               !0, 21
         56      > JMPNZ                                                    ~44, ->62
   74    57    >   IS_SMALLER                                               !0, 100
         58      > JMPNZ                                                    ~45, ->65
   82    59    >   IS_SMALLER                                               !0, 1000
         60      > JMPNZ                                                    ~46, ->78
         61    > > JMP                                                      ->94
   72    62    >   FETCH_DIM_R                                      ~47     !6, !0
         63        ASSIGN                                                   !7, ~47
   73    64      > JMP                                                      ->130
   75    65    >   DIV                                              ~49     !0, 10
         66        CAST                                          4  ~50     ~49
         67        MUL                                              ~51     ~50, 10
         68        ASSIGN                                                   !9, ~51
   76    69        MOD                                              ~53     !0, 10
         70        ASSIGN                                                   !10, ~53
   77    71        FETCH_DIM_R                                      ~55     !6, !9
         72        ASSIGN                                                   !7, ~55
   78    73      > JMPZ                                                     !10, ->77
   79    74    >   FETCH_DIM_R                                      ~57     !6, !10
         75        CONCAT                                           ~58     !1, ~57
         76        ASSIGN_OP                                     8          !7, ~58
   81    77    > > JMP                                                      ->130
   83    78    >   DIV                                              ~60     !0, 100
         79        ASSIGN                                                   !11, ~60
   84    80        MOD                                              ~62     !0, 100
         81        ASSIGN                                                   !12, ~62
   85    82        FETCH_DIM_R                                      ~64     !6, !11
         83        CONCAT                                           ~65     ~64, '+'
         84        FETCH_DIM_R                                      ~66     !6, 100
         85        CONCAT                                           ~67     ~65, ~66
         86        ASSIGN                                                   !7, ~67
   86    87      > JMPZ                                                     !12, ->93
   87    88    >   INIT_FCALL_BY_NAME                                       'convert_number_to_words'
         89        SEND_VAR_EX                                              !12
         90        DO_FCALL                                      0  $69     
         91        CONCAT                                           ~70     !2, $69
         92        ASSIGN_OP                                     8          !7, ~70
   89    93    > > JMP                                                      ->130
   91    94    >   INIT_FCALL                                               'pow'
         95        SEND_VAL                                                 1000
         96        INIT_FCALL                                               'floor'
         97        INIT_FCALL                                               'log'
         98        SEND_VAR                                                 !0
         99        SEND_VAL                                                 1000
        100        DO_ICALL                                         $72     
        101        SEND_VAR                                                 $72
        102        DO_ICALL                                         $73     
        103        SEND_VAR                                                 $73
        104        DO_ICALL                                         $74     
        105        ASSIGN                                                   !13, $74
   92   106        DIV                                              ~76     !0, !13
        107        CAST                                          4  ~77     ~76
        108        ASSIGN                                                   !14, ~77
   93   109        MOD                                              ~79     !0, !13
        110        ASSIGN                                                   !12, ~79
   94   111        INIT_FCALL_BY_NAME                                       'convert_number_to_words'
        112        SEND_VAR_EX                                              !14
        113        DO_FCALL                                      0  $81     
        114        CONCAT                                           ~82     $81, '+'
        115        FETCH_DIM_R                                      ~83     !6, !13
        116        CONCAT                                           ~84     ~82, ~83
        117        ASSIGN                                                   !7, ~84
   95   118      > JMPZ                                                     !12, ->129
   96   119    >   IS_SMALLER                                               !12, 100
        120      > JMPZ                                                     ~86, ->123
        121    >   QM_ASSIGN                                        ~87     !2
        122      > JMP                                                      ->124
        123    >   QM_ASSIGN                                        ~87     !3
        124    >   ASSIGN_OP                                     8          !7, ~87
   97   125        INIT_FCALL_BY_NAME                                       'convert_number_to_words'
        126        SEND_VAR_EX                                              !12
        127        DO_FCALL                                      0  $89     
        128        ASSIGN_OP                                     8          !7, $89
   99   129    > > JMP                                                      ->130
  102   130    >   TYPE_CHECK                                  1020  ~91     !8
        131      > JMPZ_EX                                          ~91     ~91, ->136
        132    >   INIT_FCALL                                               'is_numeric'
        133        SEND_VAR                                                 !8
        134        DO_ICALL                                         $92     
        135        BOOL                                             ~91     $92
        136    > > JMPZ                                                     ~91, ->155
  103   137    >   ASSIGN_OP                                     8          !7, !5
  104   138        ASSIGN                                                   !15, <array>
  105   139        INIT_FCALL                                               'str_split'
        140        CAST                                          6  ~95     !8
        141        SEND_VAL                                                 ~95
        142        DO_ICALL                                         $96     
        143      > FE_RESET_R                                       $97     $96, ->149
        144    > > FE_FETCH_R                                               $97, !0, ->149
  106   145    >   FETCH_DIM_R                                      ~99     !6, !0
        146        ASSIGN_DIM                                               !15
        147        OP_DATA                                                  ~99
  105   148      > JMP                                                      ->144
        149    >   FE_FREE                                                  $97
  108   150        INIT_FCALL                                               'implode'
        151        SEND_VAL                                                 '+'
        152        SEND_VAR                                                 !15
        153        DO_ICALL                                         $100    
        154        ASSIGN_OP                                     8          !7, $100
  111   155    > > RETURN                                                   !7
  112   156*     > RETURN                                                   null

End of function convert_number_to_words

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.45 ms | 1412 KiB | 33 Q