3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo "Should pass" . PHP_EOL; var_dump(to_int("0")); var_dump(to_int(0)); var_dump(to_int(0.0)); var_dump(to_int("0.0")); var_dump(to_int("10")); var_dump(to_int(10)); var_dump(to_int(10.0)); var_dump(to_int("10.0")); var_dump(to_int("10.")); echo "Should fail" . PHP_EOL; var_dump(to_int(1.5)); var_dump(to_int("1.5")); var_dump(to_int("75e-5")); var_dump(to_int("31e+7")); var_dump(to_int("10abc")); var_dump(to_int("abc10")); var_dump(to_int(INF)); var_dump(to_int(-INF)); var_dump(to_int(NAN)); var_dump(to_int(PHP_INT_MAX * 2)); var_dump(to_int(null)); var_dump(to_int(true)); var_dump(to_int(false)); var_dump(to_int(new stdClass())); var_dump(to_int(fopen("data:text/html,foobar", "r"))); var_dump(to_int([])); /** * Returns the value as an int, or false if it cannot be safely cast * @param mixed $val * @return int */ function to_int($val) { switch (gettype($val)) { case "integer": return $val; case "double": if ($val === (float) (int) $val) { return (int) $val; } else { return false; } case "string": $val = trim($val, " \t\n\r\v\f"); // trim whitespace $float = filter_var($val, FILTER_VALIDATE_FLOAT); return ($float !== false && (int) $val == $float) ? (int) $val : false; default: return false; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/r4Tai
function name:  (null)
number of ops:  159
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ECHO                                                     'Should+pass%0A'
    5     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL_BY_NAME                                       'to_int'
          3        SEND_VAL_EX                                              '0'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
    6     7        INIT_FCALL                                               'var_dump'
          8        INIT_FCALL_BY_NAME                                       'to_int'
          9        SEND_VAL_EX                                              0
         10        DO_FCALL                                      0  $2      
         11        SEND_VAR                                                 $2
         12        DO_ICALL                                                 
    7    13        INIT_FCALL                                               'var_dump'
         14        INIT_FCALL_BY_NAME                                       'to_int'
         15        SEND_VAL_EX                                              0
         16        DO_FCALL                                      0  $4      
         17        SEND_VAR                                                 $4
         18        DO_ICALL                                                 
    8    19        INIT_FCALL                                               'var_dump'
         20        INIT_FCALL_BY_NAME                                       'to_int'
         21        SEND_VAL_EX                                              '0.0'
         22        DO_FCALL                                      0  $6      
         23        SEND_VAR                                                 $6
         24        DO_ICALL                                                 
   10    25        INIT_FCALL                                               'var_dump'
         26        INIT_FCALL_BY_NAME                                       'to_int'
         27        SEND_VAL_EX                                              '10'
         28        DO_FCALL                                      0  $8      
         29        SEND_VAR                                                 $8
         30        DO_ICALL                                                 
   11    31        INIT_FCALL                                               'var_dump'
         32        INIT_FCALL_BY_NAME                                       'to_int'
         33        SEND_VAL_EX                                              10
         34        DO_FCALL                                      0  $10     
         35        SEND_VAR                                                 $10
         36        DO_ICALL                                                 
   12    37        INIT_FCALL                                               'var_dump'
         38        INIT_FCALL_BY_NAME                                       'to_int'
         39        SEND_VAL_EX                                              10
         40        DO_FCALL                                      0  $12     
         41        SEND_VAR                                                 $12
         42        DO_ICALL                                                 
   13    43        INIT_FCALL                                               'var_dump'
         44        INIT_FCALL_BY_NAME                                       'to_int'
         45        SEND_VAL_EX                                              '10.0'
         46        DO_FCALL                                      0  $14     
         47        SEND_VAR                                                 $14
         48        DO_ICALL                                                 
   14    49        INIT_FCALL                                               'var_dump'
         50        INIT_FCALL_BY_NAME                                       'to_int'
         51        SEND_VAL_EX                                              '10.'
         52        DO_FCALL                                      0  $16     
         53        SEND_VAR                                                 $16
         54        DO_ICALL                                                 
   16    55        ECHO                                                     'Should+fail%0A'
   18    56        INIT_FCALL                                               'var_dump'
         57        INIT_FCALL_BY_NAME                                       'to_int'
         58        SEND_VAL_EX                                              1.5
         59        DO_FCALL                                      0  $18     
         60        SEND_VAR                                                 $18
         61        DO_ICALL                                                 
   19    62        INIT_FCALL                                               'var_dump'
         63        INIT_FCALL_BY_NAME                                       'to_int'
         64        SEND_VAL_EX                                              '1.5'
         65        DO_FCALL                                      0  $20     
         66        SEND_VAR                                                 $20
         67        DO_ICALL                                                 
   20    68        INIT_FCALL                                               'var_dump'
         69        INIT_FCALL_BY_NAME                                       'to_int'
         70        SEND_VAL_EX                                              '75e-5'
         71        DO_FCALL                                      0  $22     
         72        SEND_VAR                                                 $22
         73        DO_ICALL                                                 
   21    74        INIT_FCALL                                               'var_dump'
         75        INIT_FCALL_BY_NAME                                       'to_int'
         76        SEND_VAL_EX                                              '31e%2B7'
         77        DO_FCALL                                      0  $24     
         78        SEND_VAR                                                 $24
         79        DO_ICALL                                                 
   23    80        INIT_FCALL                                               'var_dump'
         81        INIT_FCALL_BY_NAME                                       'to_int'
         82        SEND_VAL_EX                                              '10abc'
         83        DO_FCALL                                      0  $26     
         84        SEND_VAR                                                 $26
         85        DO_ICALL                                                 
   24    86        INIT_FCALL                                               'var_dump'
         87        INIT_FCALL_BY_NAME                                       'to_int'
         88        SEND_VAL_EX                                              'abc10'
         89        DO_FCALL                                      0  $28     
         90        SEND_VAR                                                 $28
         91        DO_ICALL                                                 
   26    92        INIT_FCALL                                               'var_dump'
         93        INIT_FCALL_BY_NAME                                       'to_int'
         94        SEND_VAL_EX                                              INF
         95        DO_FCALL                                      0  $30     
         96        SEND_VAR                                                 $30
         97        DO_ICALL                                                 
   27    98        INIT_FCALL                                               'var_dump'
         99        INIT_FCALL_BY_NAME                                       'to_int'
        100        SEND_VAL_EX                                              -INF
        101        DO_FCALL                                      0  $32     
        102        SEND_VAR                                                 $32
        103        DO_ICALL                                                 
   28   104        INIT_FCALL                                               'var_dump'
        105        INIT_FCALL_BY_NAME                                       'to_int'
        106        SEND_VAL_EX                                              NAN
        107        DO_FCALL                                      0  $34     
        108        SEND_VAR                                                 $34
        109        DO_ICALL                                                 
   29   110        INIT_FCALL                                               'var_dump'
        111        INIT_FCALL_BY_NAME                                       'to_int'
        112        SEND_VAL_EX                                              1.84467e+19
        113        DO_FCALL                                      0  $36     
        114        SEND_VAR                                                 $36
        115        DO_ICALL                                                 
   31   116        INIT_FCALL                                               'var_dump'
        117        INIT_FCALL_BY_NAME                                       'to_int'
        118        SEND_VAL_EX                                              null
        119        DO_FCALL                                      0  $38     
        120        SEND_VAR                                                 $38
        121        DO_ICALL                                                 
   32   122        INIT_FCALL                                               'var_dump'
        123        INIT_FCALL_BY_NAME                                       'to_int'
        124        SEND_VAL_EX                                              <true>
        125        DO_FCALL                                      0  $40     
        126        SEND_VAR                                                 $40
        127        DO_ICALL                                                 
   33   128        INIT_FCALL                                               'var_dump'
        129        INIT_FCALL_BY_NAME                                       'to_int'
        130        SEND_VAL_EX                                              <false>
        131        DO_FCALL                                      0  $42     
        132        SEND_VAR                                                 $42
        133        DO_ICALL                                                 
   34   134        INIT_FCALL                                               'var_dump'
        135        INIT_FCALL_BY_NAME                                       'to_int'
        136        NEW                                              $44     'stdClass'
        137        DO_FCALL                                      0          
        138        SEND_VAR_NO_REF_EX                                       $44
        139        DO_FCALL                                      0  $46     
        140        SEND_VAR                                                 $46
        141        DO_ICALL                                                 
   35   142        INIT_FCALL                                               'var_dump'
        143        INIT_FCALL_BY_NAME                                       'to_int'
        144        INIT_FCALL                                               'fopen'
        145        SEND_VAL                                                 'data%3Atext%2Fhtml%2Cfoobar'
        146        SEND_VAL                                                 'r'
        147        DO_ICALL                                         $48     
        148        SEND_VAR_NO_REF_EX                                       $48
        149        DO_FCALL                                      0  $49     
        150        SEND_VAR                                                 $49
        151        DO_ICALL                                                 
   36   152        INIT_FCALL                                               'var_dump'
        153        INIT_FCALL_BY_NAME                                       'to_int'
        154        SEND_VAL_EX                                              <array>
        155        DO_FCALL                                      0  $51     
        156        SEND_VAR                                                 $51
        157        DO_ICALL                                                 
   61   158      > RETURN                                                   1

Function to_int:
Finding entry points
Branch analysis from position: 0
5 jumps found. (Code = 188) Position 1 = 10, Position 2 = 12, Position 3 = 22, Position 4 = 44, Position 5 = 3
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 20
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 46) Position 1 = 34, Position 2 = 37
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 41
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 9, Position 2 = 22
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 22
Branch analysis from position: 12
Branch analysis from position: 10
filename:       /in/r4Tai
function name:  to_int
number of ops:  48
compiled vars:  !0 = $val, !1 = $float
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   45     1        GET_TYPE                                         ~2      !0
          2      > SWITCH_STRING                                            ~2, [ 'integer':->10, 'double':->12, 'string':->22, ], ->44
   46     3    >   CASE                                                     ~2, 'integer'
          4      > JMPNZ                                                    ~3, ->10
   48     5    >   CASE                                                     ~2, 'double'
          6      > JMPNZ                                                    ~3, ->12
   54     7    >   CASE                                                     ~2, 'string'
          8      > JMPNZ                                                    ~3, ->22
          9    > > JMP                                                      ->44
   47    10    >   FREE                                                     ~2
         11      > RETURN                                                   !0
   49    12    >   CAST                                          4  ~4      !0
         13        CAST                                          5  ~5      ~4
         14        IS_IDENTICAL                                             !0, ~5
         15      > JMPZ                                                     ~6, ->20
   50    16    >   CAST                                          4  ~7      !0
         17        FREE                                                     ~2
         18      > RETURN                                                   ~7
         19*       JMP                                                      ->22
   52    20    >   FREE                                                     ~2
         21      > RETURN                                                   <false>
   55    22    >   INIT_FCALL                                               'trim'
         23        SEND_VAR                                                 !0
         24        SEND_VAL                                                 '+%09%0A%0D%0B%0C'
         25        DO_ICALL                                         $8      
         26        ASSIGN                                                   !0, $8
   56    27        INIT_FCALL                                               'filter_var'
         28        SEND_VAR                                                 !0
         29        SEND_VAL                                                 259
         30        DO_ICALL                                         $10     
         31        ASSIGN                                                   !1, $10
   57    32        TYPE_CHECK                                  1018  ~12     !1
         33      > JMPZ_EX                                          ~12     ~12, ->37
         34    >   CAST                                          4  ~13     !0
         35        IS_EQUAL                                         ~14     !1, ~13
         36        BOOL                                             ~12     ~14
         37    > > JMPZ                                                     ~12, ->41
         38    >   CAST                                          4  ~15     !0
         39        QM_ASSIGN                                        ~16     ~15
         40      > JMP                                                      ->42
         41    >   QM_ASSIGN                                        ~16     <false>
         42    >   FREE                                                     ~2
         43      > RETURN                                                   ~16
   59    44    >   FREE                                                     ~2
         45      > RETURN                                                   <false>
         46*       FREE                                                     ~2
   61    47*     > RETURN                                                   null

End of function to_int

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.64 ms | 1412 KiB | 21 Q