3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo "<pre>"; 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")); echo "Should fail" . PHP_EOL; var_dump(to_int(1.5)); var_dump(to_int("1.5")); 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([])); echo "</pre>"; /** * 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 (!is_infinite($val) && !is_nan($val) && $val >= PHP_INT_MIN) { // due to rounding issues, on 64-bit platforms // the float must be less than PHP_INT_MAX if ( (PHP_INT_SIZE === 8 && $val < PHP_INT_MAX) || // valid 64-bit (PHP_INT_SIZE !== 8 && $val <= PHP_INT_MAX) // valid non-64-bit ) { // only accept the float if it can be cast to int without data loss $int = (int) $val; if ($int == $val) { return $int; } } } return false; case "string": $val = trim($val, " \t\n\r\v\f"); // trim whitespace $float = filter_var($val, FILTER_VALIDATE_FLOAT); if ($float !== false) { // only accept the float if it can be cast to int without data loss $int = (int) $float; return ($int == $float) ? $int : false; } default: return false; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CjTPO
function name:  (null)
number of ops:  143
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ECHO                                                     '%3Cpre%3E'
    5     1        ECHO                                                     'Should+pass%0A'
    7     2        INIT_FCALL                                               'var_dump'
          3        INIT_FCALL_BY_NAME                                       'to_int'
          4        SEND_VAL_EX                                              '0'
          5        DO_FCALL                                      0  $0      
          6        SEND_VAR                                                 $0
          7        DO_ICALL                                                 
    8     8        INIT_FCALL                                               'var_dump'
          9        INIT_FCALL_BY_NAME                                       'to_int'
         10        SEND_VAL_EX                                              0
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
    9    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL_BY_NAME                                       'to_int'
         16        SEND_VAL_EX                                              0
         17        DO_FCALL                                      0  $4      
         18        SEND_VAR                                                 $4
         19        DO_ICALL                                                 
   10    20        INIT_FCALL                                               'var_dump'
         21        INIT_FCALL_BY_NAME                                       'to_int'
         22        SEND_VAL_EX                                              '0.0'
         23        DO_FCALL                                      0  $6      
         24        SEND_VAR                                                 $6
         25        DO_ICALL                                                 
   12    26        INIT_FCALL                                               'var_dump'
         27        INIT_FCALL_BY_NAME                                       'to_int'
         28        SEND_VAL_EX                                              '10'
         29        DO_FCALL                                      0  $8      
         30        SEND_VAR                                                 $8
         31        DO_ICALL                                                 
   13    32        INIT_FCALL                                               'var_dump'
         33        INIT_FCALL_BY_NAME                                       'to_int'
         34        SEND_VAL_EX                                              10
         35        DO_FCALL                                      0  $10     
         36        SEND_VAR                                                 $10
         37        DO_ICALL                                                 
   14    38        INIT_FCALL                                               'var_dump'
         39        INIT_FCALL_BY_NAME                                       'to_int'
         40        SEND_VAL_EX                                              10
         41        DO_FCALL                                      0  $12     
         42        SEND_VAR                                                 $12
         43        DO_ICALL                                                 
   15    44        INIT_FCALL                                               'var_dump'
         45        INIT_FCALL_BY_NAME                                       'to_int'
         46        SEND_VAL_EX                                              '10.0'
         47        DO_FCALL                                      0  $14     
         48        SEND_VAR                                                 $14
         49        DO_ICALL                                                 
   17    50        ECHO                                                     'Should+fail%0A'
   19    51        INIT_FCALL                                               'var_dump'
         52        INIT_FCALL_BY_NAME                                       'to_int'
         53        SEND_VAL_EX                                              1.5
         54        DO_FCALL                                      0  $16     
         55        SEND_VAR                                                 $16
         56        DO_ICALL                                                 
   20    57        INIT_FCALL                                               'var_dump'
         58        INIT_FCALL_BY_NAME                                       'to_int'
         59        SEND_VAL_EX                                              '1.5'
         60        DO_FCALL                                      0  $18     
         61        SEND_VAR                                                 $18
         62        DO_ICALL                                                 
   22    63        INIT_FCALL                                               'var_dump'
         64        INIT_FCALL_BY_NAME                                       'to_int'
         65        SEND_VAL_EX                                              '10abc'
         66        DO_FCALL                                      0  $20     
         67        SEND_VAR                                                 $20
         68        DO_ICALL                                                 
   23    69        INIT_FCALL                                               'var_dump'
         70        INIT_FCALL_BY_NAME                                       'to_int'
         71        SEND_VAL_EX                                              'abc10'
         72        DO_FCALL                                      0  $22     
         73        SEND_VAR                                                 $22
         74        DO_ICALL                                                 
   25    75        INIT_FCALL                                               'var_dump'
         76        INIT_FCALL_BY_NAME                                       'to_int'
         77        SEND_VAL_EX                                              INF
         78        DO_FCALL                                      0  $24     
         79        SEND_VAR                                                 $24
         80        DO_ICALL                                                 
   26    81        INIT_FCALL                                               'var_dump'
         82        INIT_FCALL_BY_NAME                                       'to_int'
         83        SEND_VAL_EX                                              -INF
         84        DO_FCALL                                      0  $26     
         85        SEND_VAR                                                 $26
         86        DO_ICALL                                                 
   27    87        INIT_FCALL                                               'var_dump'
         88        INIT_FCALL_BY_NAME                                       'to_int'
         89        SEND_VAL_EX                                              NAN
         90        DO_FCALL                                      0  $28     
         91        SEND_VAR                                                 $28
         92        DO_ICALL                                                 
   28    93        INIT_FCALL                                               'var_dump'
         94        INIT_FCALL_BY_NAME                                       'to_int'
         95        SEND_VAL_EX                                              1.84467e+19
         96        DO_FCALL                                      0  $30     
         97        SEND_VAR                                                 $30
         98        DO_ICALL                                                 
   30    99        INIT_FCALL                                               'var_dump'
        100        INIT_FCALL_BY_NAME                                       'to_int'
        101        SEND_VAL_EX                                              null
        102        DO_FCALL                                      0  $32     
        103        SEND_VAR                                                 $32
        104        DO_ICALL                                                 
   31   105        INIT_FCALL                                               'var_dump'
        106        INIT_FCALL_BY_NAME                                       'to_int'
        107        SEND_VAL_EX                                              <true>
        108        DO_FCALL                                      0  $34     
        109        SEND_VAR                                                 $34
        110        DO_ICALL                                                 
   32   111        INIT_FCALL                                               'var_dump'
        112        INIT_FCALL_BY_NAME                                       'to_int'
        113        SEND_VAL_EX                                              <false>
        114        DO_FCALL                                      0  $36     
        115        SEND_VAR                                                 $36
        116        DO_ICALL                                                 
   33   117        INIT_FCALL                                               'var_dump'
        118        INIT_FCALL_BY_NAME                                       'to_int'
        119        NEW                                              $38     'stdClass'
        120        DO_FCALL                                      0          
        121        SEND_VAR_NO_REF_EX                                       $38
        122        DO_FCALL                                      0  $40     
        123        SEND_VAR                                                 $40
        124        DO_ICALL                                                 
   34   125        INIT_FCALL                                               'var_dump'
        126        INIT_FCALL_BY_NAME                                       'to_int'
        127        INIT_FCALL                                               'fopen'
        128        SEND_VAL                                                 'data%3Atext%2Fhtml%2Cfoobar'
        129        SEND_VAL                                                 'r'
        130        DO_ICALL                                         $42     
        131        SEND_VAR_NO_REF_EX                                       $42
        132        DO_FCALL                                      0  $43     
        133        SEND_VAR                                                 $43
        134        DO_ICALL                                                 
   35   135        INIT_FCALL                                               'var_dump'
        136        INIT_FCALL_BY_NAME                                       'to_int'
        137        SEND_VAL_EX                                              <array>
        138        DO_FCALL                                      0  $45     
        139        SEND_VAR                                                 $45
        140        DO_ICALL                                                 
   37   141        ECHO                                                     '%3C%2Fpre%3E'
   79   142      > 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 = 39, Position 4 = 60, 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 = 46) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
2 jumps found. (Code = 46) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 37
Branch analysis from position: 26
2 jumps found. (Code = 47) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 37
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
Branch analysis from position: 30
Branch analysis from position: 37
Branch analysis from position: 25
Branch analysis from position: 22
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 60
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 57
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
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 = 39
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 39
Branch analysis from position: 12
Branch analysis from position: 10
filename:       /in/CjTPO
function name:  to_int
number of ops:  64
compiled vars:  !0 = $val, !1 = $int, !2 = $float
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        GET_TYPE                                         ~3      !0
          2      > SWITCH_STRING                                            ~3, [ 'integer':->10, 'double':->12, 'string':->39, ], ->60
   47     3    >   CASE                                                     ~3, 'integer'
          4      > JMPNZ                                                    ~4, ->10
   49     5    >   CASE                                                     ~3, 'double'
          6      > JMPNZ                                                    ~4, ->12
   68     7    >   CASE                                                     ~3, 'string'
          8      > JMPNZ                                                    ~4, ->39
          9    > > JMP                                                      ->60
   48    10    >   FREE                                                     ~3
         11      > RETURN                                                   !0
   50    12    >   INIT_FCALL                                               'is_infinite'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $5      
         15        BOOL_NOT                                         ~6      $5
         16      > JMPZ_EX                                          ~6      ~6, ->22
         17    >   INIT_FCALL                                               'is_nan'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $7      
         20        BOOL_NOT                                         ~8      $7
         21        BOOL                                             ~6      ~8
         22    > > JMPZ_EX                                          ~6      ~6, ->25
         23    >   IS_SMALLER_OR_EQUAL                              ~9      -9223372036854775808, !0
         24        BOOL                                             ~6      ~9
         25    > > JMPZ                                                     ~6, ->37
   55    26    >   IS_SMALLER                                       ~10     !0, 9223372036854775807
         27        BOOL                                             ~11     ~10
         28      > JMPNZ_EX                                         ~11     ~11, ->30
   56    29    >   BOOL                                             ~11     <false>
         30    > > JMPZ                                                     ~11, ->37
   59    31    >   CAST                                          4  ~12     !0
         32        ASSIGN                                                   !1, ~12
   61    33        IS_EQUAL                                                 !1, !0
         34      > JMPZ                                                     ~14, ->37
   62    35    >   FREE                                                     ~3
         36      > RETURN                                                   !1
   67    37    >   FREE                                                     ~3
         38      > RETURN                                                   <false>
   69    39    >   INIT_FCALL                                               'trim'
         40        SEND_VAR                                                 !0
         41        SEND_VAL                                                 '+%09%0A%0D%0B%0C'
         42        DO_ICALL                                         $15     
         43        ASSIGN                                                   !0, $15
   70    44        INIT_FCALL                                               'filter_var'
         45        SEND_VAR                                                 !0
         46        SEND_VAL                                                 259
         47        DO_ICALL                                         $17     
         48        ASSIGN                                                   !2, $17
   71    49        TYPE_CHECK                                  1018          !2
         50      > JMPZ                                                     ~19, ->60
   73    51    >   CAST                                          4  ~20     !2
         52        ASSIGN                                                   !1, ~20
   74    53        IS_EQUAL                                                 !1, !2
         54      > JMPZ                                                     ~22, ->57
         55    >   QM_ASSIGN                                        ~23     !1
         56      > JMP                                                      ->58
         57    >   QM_ASSIGN                                        ~23     <false>
         58    >   FREE                                                     ~3
         59      > RETURN                                                   ~23
   77    60    >   FREE                                                     ~3
         61      > RETURN                                                   <false>
         62*       FREE                                                     ~3
   79    63*     > RETURN                                                   null

End of function to_int

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.63 ms | 1412 KiB | 25 Q