3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @param string format Sets format for size. * Format should containt string parseable by sprintf function and contain one %F macro that will be replaced by size. Another macro is U/u. It will be replaced with used unit. U for uppercase, u - lowercase. If 'i' is present at the end of format string, size multiplier will be set to 1024 (and units be KiB, MiB and so on), otherwise multiplier is set to 1000. * @example "%.0F Ui" 617 KiB * @example "%.3F Ui" 617.070 KiB * @example "%10.3F Ui" 616.85 KiB * @example "%.3F U" 632.096 KB * * @param integer $bytes Size in bytes * @param string $unit Sets default unit. Can have these values: B, KB, MG, GB, TB, PB, EB, ZB and YB */ function size_format($format, $bytes, $unit = null) { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); $bytes = max($bytes, 0); if ($unit !== null) $unit = strtoupper($unit); if (substr($format, -1) === 'i') { $multiplier = 1024; $format = substr($format, 0, -1); } else $multiplier = 1000; if ($unit === null || !in_array($unit, $units)) { $pow = floor(($bytes ? log($bytes) : 0) / log($multiplier)); $pow = min($pow, count($units) - 1); $bytes /= pow($multiplier, $pow); $unit = $units[$pow]; } else { $pow = array_search($unit, $units); $bytes /= pow($multiplier, $pow); } if ($multiplier == 1024) $unit = (strlen($unit) == 2) ? substr($unit, 0, 1).'iB' : $unit; if (strpos($format, 'u') !== false) $format = str_replace('u', strtolower($unit), $format); else $format = str_replace('U', $unit, $format); return sprintf($format, $bytes); } var_dump(PHP_INT_MAX); $s = '9223372036854775808'; $i = (int)$s; var_dump($s, $i); var_dump(size_format('%.2F Ui', $s)); var_dump(size_format('%.2F Ui', $i));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LuL4U
function name:  (null)
number of ops:  25
compiled vars:  !0 = $s, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 9223372036854775807
          2        DO_ICALL                                                 
   44     3        ASSIGN                                                   !0, '9223372036854775808'
   45     4        CAST                                          4  ~4      !0
          5        ASSIGN                                                   !1, ~4
   46     6        INIT_FCALL                                               'var_dump'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                                 
   48    10        INIT_FCALL                                               'var_dump'
         11        INIT_FCALL                                               'size_format'
         12        SEND_VAL                                                 '%25.2F+Ui'
         13        SEND_VAR                                                 !0
         14        DO_FCALL                                      0  $7      
         15        SEND_VAR                                                 $7
         16        DO_ICALL                                                 
   49    17        INIT_FCALL                                               'var_dump'
         18        INIT_FCALL                                               'size_format'
         19        SEND_VAL                                                 '%25.2F+Ui'
         20        SEND_VAR                                                 !1
         21        DO_FCALL                                      0  $9      
         22        SEND_VAR                                                 $9
         23        DO_ICALL                                                 
         24      > RETURN                                                   1

Function size_format:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 29
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 47) Position 1 = 32, Position 2 = 38
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 69
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 46
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 94
Branch analysis from position: 81
2 jumps found. (Code = 43) Position 1 = 84, Position 2 = 92
Branch analysis from position: 84
1 jumps found. (Code = 42) Position 1 = 93
Branch analysis from position: 93
2 jumps found. (Code = 43) Position 1 = 100, Position 2 = 110
Branch analysis from position: 100
1 jumps found. (Code = 42) Position 1 = 116
Branch analysis from position: 116
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 110
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 92
2 jumps found. (Code = 43) Position 1 = 100, Position 2 = 110
Branch analysis from position: 100
Branch analysis from position: 110
Branch analysis from position: 94
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 94
Branch analysis from position: 81
Branch analysis from position: 94
Branch analysis from position: 38
Branch analysis from position: 29
2 jumps found. (Code = 47) Position 1 = 32, Position 2 = 38
Branch analysis from position: 32
Branch analysis from position: 38
Branch analysis from position: 15
filename:       /in/LuL4U
function name:  size_format
number of ops:  122
compiled vars:  !0 = $format, !1 = $bytes, !2 = $unit, !3 = $units, !4 = $multiplier, !5 = $pow
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
   15     3        ASSIGN                                                   !3, <array>
   16     4        INIT_FCALL                                               'max'
          5        SEND_VAR                                                 !1
          6        SEND_VAL                                                 0
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !1, $7
   17     9        TYPE_CHECK                                  1020          !2
         10      > JMPZ                                                     ~9, ->15
   18    11    >   INIT_FCALL                                               'strtoupper'
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                         $10     
         14        ASSIGN                                                   !2, $10
   19    15    >   INIT_FCALL                                               'substr'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 -1
         18        DO_ICALL                                         $12     
         19        IS_IDENTICAL                                             $12, 'i'
         20      > JMPZ                                                     ~13, ->29
   20    21    >   ASSIGN                                                   !4, 1024
   21    22        INIT_FCALL                                               'substr'
         23        SEND_VAR                                                 !0
         24        SEND_VAL                                                 0
         25        SEND_VAL                                                 -1
         26        DO_ICALL                                         $15     
         27        ASSIGN                                                   !0, $15
         28      > JMP                                                      ->30
   24    29    >   ASSIGN                                                   !4, 1000
   25    30    >   TYPE_CHECK                                    2  ~18     !2
         31      > JMPNZ_EX                                         ~18     ~18, ->38
         32    >   INIT_FCALL                                               'in_array'
         33        SEND_VAR                                                 !2
         34        SEND_VAR                                                 !3
         35        DO_ICALL                                         $19     
         36        BOOL_NOT                                         ~20     $19
         37        BOOL                                             ~18     ~20
         38    > > JMPZ                                                     ~18, ->69
   26    39    >   INIT_FCALL                                               'floor'
         40      > JMPZ                                                     !1, ->46
         41    >   INIT_FCALL                                               'log'
         42        SEND_VAR                                                 !1
         43        DO_ICALL                                         $21     
         44        QM_ASSIGN                                        ~22     $21
         45      > JMP                                                      ->47
         46    >   QM_ASSIGN                                        ~22     0
         47    >   INIT_FCALL                                               'log'
         48        SEND_VAR                                                 !4
         49        DO_ICALL                                         $23     
         50        DIV                                              ~24     ~22, $23
         51        SEND_VAL                                                 ~24
         52        DO_ICALL                                         $25     
         53        ASSIGN                                                   !5, $25
   27    54        INIT_FCALL                                               'min'
         55        SEND_VAR                                                 !5
         56        COUNT                                            ~27     !3
         57        SUB                                              ~28     ~27, 1
         58        SEND_VAL                                                 ~28
         59        DO_ICALL                                         $29     
         60        ASSIGN                                                   !5, $29
   28    61        INIT_FCALL                                               'pow'
         62        SEND_VAR                                                 !4
         63        SEND_VAR                                                 !5
         64        DO_ICALL                                         $31     
         65        ASSIGN_OP                                     4          !1, $31
   29    66        FETCH_DIM_R                                      ~33     !3, !5
         67        ASSIGN                                                   !2, ~33
         68      > JMP                                                      ->79
   31    69    >   INIT_FCALL                                               'array_search'
         70        SEND_VAR                                                 !2
         71        SEND_VAR                                                 !3
         72        DO_ICALL                                         $35     
         73        ASSIGN                                                   !5, $35
   32    74        INIT_FCALL                                               'pow'
         75        SEND_VAR                                                 !4
         76        SEND_VAR                                                 !5
         77        DO_ICALL                                         $37     
         78        ASSIGN_OP                                     4          !1, $37
   34    79    >   IS_EQUAL                                                 !4, 1024
         80      > JMPZ                                                     ~39, ->94
   35    81    >   STRLEN                                           ~40     !2
         82        IS_EQUAL                                                 ~40, 2
         83      > JMPZ                                                     ~41, ->92
         84    >   INIT_FCALL                                               'substr'
         85        SEND_VAR                                                 !2
         86        SEND_VAL                                                 0
         87        SEND_VAL                                                 1
         88        DO_ICALL                                         $42     
         89        CONCAT                                           ~43     $42, 'iB'
         90        QM_ASSIGN                                        ~44     ~43
         91      > JMP                                                      ->93
         92    >   QM_ASSIGN                                        ~44     !2
         93    >   ASSIGN                                                   !2, ~44
   36    94    >   INIT_FCALL                                               'strpos'
         95        SEND_VAR                                                 !0
         96        SEND_VAL                                                 'u'
         97        DO_ICALL                                         $46     
         98        TYPE_CHECK                                  1018          $46
         99      > JMPZ                                                     ~47, ->110
   37   100    >   INIT_FCALL                                               'str_replace'
        101        SEND_VAL                                                 'u'
        102        INIT_FCALL                                               'strtolower'
        103        SEND_VAR                                                 !2
        104        DO_ICALL                                         $48     
        105        SEND_VAR                                                 $48
        106        SEND_VAR                                                 !0
        107        DO_ICALL                                         $49     
        108        ASSIGN                                                   !0, $49
        109      > JMP                                                      ->116
   39   110    >   INIT_FCALL                                               'str_replace'
        111        SEND_VAL                                                 'U'
        112        SEND_VAR                                                 !2
        113        SEND_VAR                                                 !0
        114        DO_ICALL                                         $51     
        115        ASSIGN                                                   !0, $51
   40   116    >   INIT_FCALL                                               'sprintf'
        117        SEND_VAR                                                 !0
        118        SEND_VAR                                                 !1
        119        DO_ICALL                                         $53     
        120      > RETURN                                                   $53
   41   121*     > RETURN                                                   null

End of function size_format

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
188.03 ms | 1411 KiB | 43 Q