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 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/scYhF
function name:  (null)
number of ops:  146
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+pass%0A'
   11     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL_BY_NAME                                       'to_int'
          8        SEND_VAL_EX                                              '0'
          9        DO_FCALL                                      0  $1      
         10        SEND_VAR                                                 $1
         11        DO_ICALL                                                 
   12    12        INIT_FCALL                                               'var_dump'
         13        INIT_FCALL_BY_NAME                                       'to_int'
         14        SEND_VAL_EX                                              0
         15        DO_FCALL                                      0  $3      
         16        SEND_VAR                                                 $3
         17        DO_ICALL                                                 
   13    18        INIT_FCALL                                               'var_dump'
         19        INIT_FCALL_BY_NAME                                       'to_int'
         20        SEND_VAL_EX                                              0
         21        DO_FCALL                                      0  $5      
         22        SEND_VAR                                                 $5
         23        DO_ICALL                                                 
   14    24        INIT_FCALL                                               'var_dump'
         25        INIT_FCALL_BY_NAME                                       'to_int'
         26        SEND_VAL_EX                                              '0.0'
         27        DO_FCALL                                      0  $7      
         28        SEND_VAR                                                 $7
         29        DO_ICALL                                                 
   16    30        INIT_FCALL                                               'var_dump'
         31        INIT_FCALL_BY_NAME                                       'to_int'
         32        SEND_VAL_EX                                              '10'
         33        DO_FCALL                                      0  $9      
         34        SEND_VAR                                                 $9
         35        DO_ICALL                                                 
   17    36        INIT_FCALL                                               'var_dump'
         37        INIT_FCALL_BY_NAME                                       'to_int'
         38        SEND_VAL_EX                                              10
         39        DO_FCALL                                      0  $11     
         40        SEND_VAR                                                 $11
         41        DO_ICALL                                                 
   18    42        INIT_FCALL                                               'var_dump'
         43        INIT_FCALL_BY_NAME                                       'to_int'
         44        SEND_VAL_EX                                              10
         45        DO_FCALL                                      0  $13     
         46        SEND_VAR                                                 $13
         47        DO_ICALL                                                 
   19    48        INIT_FCALL                                               'var_dump'
         49        INIT_FCALL_BY_NAME                                       'to_int'
         50        SEND_VAL_EX                                              '10.0'
         51        DO_FCALL                                      0  $15     
         52        SEND_VAR                                                 $15
         53        DO_ICALL                                                 
   21    54        ECHO                                                     'Should+fail%0A'
   23    55        INIT_FCALL                                               'var_dump'
         56        INIT_FCALL_BY_NAME                                       'to_int'
         57        SEND_VAL_EX                                              1.5
         58        DO_FCALL                                      0  $17     
         59        SEND_VAR                                                 $17
         60        DO_ICALL                                                 
   24    61        INIT_FCALL                                               'var_dump'
         62        INIT_FCALL_BY_NAME                                       'to_int'
         63        SEND_VAL_EX                                              '1.5'
         64        DO_FCALL                                      0  $19     
         65        SEND_VAR                                                 $19
         66        DO_ICALL                                                 
   26    67        INIT_FCALL                                               'var_dump'
         68        INIT_FCALL_BY_NAME                                       'to_int'
         69        SEND_VAL_EX                                              '10abc'
         70        DO_FCALL                                      0  $21     
         71        SEND_VAR                                                 $21
         72        DO_ICALL                                                 
   27    73        INIT_FCALL                                               'var_dump'
         74        INIT_FCALL_BY_NAME                                       'to_int'
         75        SEND_VAL_EX                                              'abc10'
         76        DO_FCALL                                      0  $23     
         77        SEND_VAR                                                 $23
         78        DO_ICALL                                                 
   29    79        INIT_FCALL                                               'var_dump'
         80        INIT_FCALL_BY_NAME                                       'to_int'
         81        SEND_VAL_EX                                              INF
         82        DO_FCALL                                      0  $25     
         83        SEND_VAR                                                 $25
         84        DO_ICALL                                                 
   30    85        INIT_FCALL                                               'var_dump'
         86        INIT_FCALL_BY_NAME                                       'to_int'
         87        SEND_VAL_EX                                              -INF
         88        DO_FCALL                                      0  $27     
         89        SEND_VAR                                                 $27
         90        DO_ICALL                                                 
   31    91        INIT_FCALL                                               'var_dump'
         92        INIT_FCALL_BY_NAME                                       'to_int'
         93        SEND_VAL_EX                                              NAN
         94        DO_FCALL                                      0  $29     
         95        SEND_VAR                                                 $29
         96        DO_ICALL                                                 
   32    97        INIT_FCALL                                               'var_dump'
         98        INIT_FCALL_BY_NAME                                       'to_int'
         99        SEND_VAL_EX                                              1.84467e+19
        100        DO_FCALL                                      0  $31     
        101        SEND_VAR                                                 $31
        102        DO_ICALL                                                 
   34   103        INIT_FCALL                                               'var_dump'
        104        INIT_FCALL_BY_NAME                                       'to_int'
        105        SEND_VAL_EX                                              null
        106        DO_FCALL                                      0  $33     
        107        SEND_VAR                                                 $33
        108        DO_ICALL                                                 
   35   109        INIT_FCALL                                               'var_dump'
        110        INIT_FCALL_BY_NAME                                       'to_int'
        111        SEND_VAL_EX                                              <true>
        112        DO_FCALL                                      0  $35     
        113        SEND_VAR                                                 $35
        114        DO_ICALL                                                 
   36   115        INIT_FCALL                                               'var_dump'
        116        INIT_FCALL_BY_NAME                                       'to_int'
        117        SEND_VAL_EX                                              <false>
        118        DO_FCALL                                      0  $37     
        119        SEND_VAR                                                 $37
        120        DO_ICALL                                                 
   37   121        INIT_FCALL                                               'var_dump'
        122        INIT_FCALL_BY_NAME                                       'to_int'
        123        NEW                                              $39     'stdClass'
        124        DO_FCALL                                      0          
        125        SEND_VAR_NO_REF_EX                                       $39
        126        DO_FCALL                                      0  $41     
        127        SEND_VAR                                                 $41
        128        DO_ICALL                                                 
   38   129        INIT_FCALL                                               'var_dump'
        130        INIT_FCALL_BY_NAME                                       'to_int'
        131        INIT_FCALL                                               'fopen'
        132        SEND_VAL                                                 'data%3Atext%2Fhtml%2Cfoobar'
        133        SEND_VAL                                                 'r'
        134        DO_ICALL                                         $43     
        135        SEND_VAR_NO_REF_EX                                       $43
        136        DO_FCALL                                      0  $44     
        137        SEND_VAR                                                 $44
        138        DO_ICALL                                                 
   39   139        INIT_FCALL                                               'var_dump'
        140        INIT_FCALL_BY_NAME                                       'to_int'
        141        SEND_VAL_EX                                              <array>
        142        DO_FCALL                                      0  $46     
        143        SEND_VAR                                                 $46
        144        DO_ICALL                                                 
   81   145      > 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/scYhF
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
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   48     1        GET_TYPE                                         ~3      !0
          2      > SWITCH_STRING                                            ~3, [ 'integer':->10, 'double':->12, 'string':->39, ], ->60
   49     3    >   CASE                                                     ~3, 'integer'
          4      > JMPNZ                                                    ~4, ->10
   51     5    >   CASE                                                     ~3, 'double'
          6      > JMPNZ                                                    ~4, ->12
   70     7    >   CASE                                                     ~3, 'string'
          8      > JMPNZ                                                    ~4, ->39
          9    > > JMP                                                      ->60
   50    10    >   FREE                                                     ~3
         11      > RETURN                                                   !0
   52    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
   57    26    >   IS_SMALLER                                       ~10     !0, 9223372036854775807
         27        BOOL                                             ~11     ~10
         28      > JMPNZ_EX                                         ~11     ~11, ->30
   58    29    >   BOOL                                             ~11     <false>
         30    > > JMPZ                                                     ~11, ->37
   61    31    >   CAST                                          4  ~12     !0
         32        ASSIGN                                                   !1, ~12
   63    33        IS_EQUAL                                                 !1, !0
         34      > JMPZ                                                     ~14, ->37
   64    35    >   FREE                                                     ~3
         36      > RETURN                                                   !1
   69    37    >   FREE                                                     ~3
         38      > RETURN                                                   <false>
   71    39    >   INIT_FCALL                                               'trim'
         40        SEND_VAR                                                 !0
         41        SEND_VAL                                                 '+%09%0A%0D%0B%0C'
         42        DO_ICALL                                         $15     
         43        ASSIGN                                                   !0, $15
   72    44        INIT_FCALL                                               'filter_var'
         45        SEND_VAR                                                 !0
         46        SEND_VAL                                                 259
         47        DO_ICALL                                         $17     
         48        ASSIGN                                                   !2, $17
   73    49        TYPE_CHECK                                  1018          !2
         50      > JMPZ                                                     ~19, ->60
   75    51    >   CAST                                          4  ~20     !2
         52        ASSIGN                                                   !1, ~20
   76    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
   79    60    >   FREE                                                     ~3
         61      > RETURN                                                   <false>
         62*       FREE                                                     ~3
   81    63*     > RETURN                                                   null

End of function to_int

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.34 ms | 1412 KiB | 27 Q