3v4l.org

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

End of function to_int

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.55 ms | 1408 KiB | 27 Q