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); } var_dump(to_int((float) PHP_INT_MAX) === PHP_INT_MAX); /** * 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 ) { return (int) $val; // valid floats should cast } } return false; case "string": $val = trim($val, " \t\n\r\v\f"); // trim whitespace return filter_var($val, FILTER_VALIDATE_INT); 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/A4TIW
function name:  (null)
number of ops:  14
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    >   INIT_FCALL                                               'var_dump'
          6        INIT_FCALL_BY_NAME                                       'to_int'
          7        CAST                                          5  ~1      9223372036854775807
          8        SEND_VAL_EX                                              ~1
          9        DO_FCALL                                      0  $2      
         10        IS_IDENTICAL                                     ~3      $2, 9223372036854775807
         11        SEND_VAL                                                 ~3
         12        DO_ICALL                                                 
   41    13      > 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 = 36, Position 4 = 47, 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 = 34
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 = 34
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Branch analysis from position: 34
Branch analysis from position: 25
Branch analysis from position: 22
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
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 = 36
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 36
Branch analysis from position: 12
Branch analysis from position: 10
filename:       /in/A4TIW
function name:  to_int
number of ops:  51
compiled vars:  !0 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
   18     1        GET_TYPE                                         ~1      !0
          2      > SWITCH_STRING                                            ~1, [ 'integer':->10, 'double':->12, 'string':->36, ], ->47
   19     3    >   CASE                                                     ~1, 'integer'
          4      > JMPNZ                                                    ~2, ->10
   21     5    >   CASE                                                     ~1, 'double'
          6      > JMPNZ                                                    ~2, ->12
   35     7    >   CASE                                                     ~1, 'string'
          8      > JMPNZ                                                    ~2, ->36
          9    > > JMP                                                      ->47
   20    10    >   FREE                                                     ~1
         11      > RETURN                                                   !0
   22    12    >   INIT_FCALL                                               'is_infinite'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $3      
         15        BOOL_NOT                                         ~4      $3
         16      > JMPZ_EX                                          ~4      ~4, ->22
         17    >   INIT_FCALL                                               'is_nan'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $5      
         20        BOOL_NOT                                         ~6      $5
         21        BOOL                                             ~4      ~6
         22    > > JMPZ_EX                                          ~4      ~4, ->25
         23    >   IS_SMALLER_OR_EQUAL                              ~7      -9223372036854775808, !0
         24        BOOL                                             ~4      ~7
         25    > > JMPZ                                                     ~4, ->34
   27    26    >   IS_SMALLER                                       ~8      !0, 9223372036854775807
         27        BOOL                                             ~9      ~8
         28      > JMPNZ_EX                                         ~9      ~9, ->30
   28    29    >   BOOL                                             ~9      <false>
         30    > > JMPZ                                                     ~9, ->34
   30    31    >   CAST                                          4  ~10     !0
         32        FREE                                                     ~1
         33      > RETURN                                                   ~10
   34    34    >   FREE                                                     ~1
         35      > RETURN                                                   <false>
   36    36    >   INIT_FCALL                                               'trim'
         37        SEND_VAR                                                 !0
         38        SEND_VAL                                                 '+%09%0A%0D%0B%0C'
         39        DO_ICALL                                         $11     
         40        ASSIGN                                                   !0, $11
   37    41        INIT_FCALL                                               'filter_var'
         42        SEND_VAR                                                 !0
         43        SEND_VAL                                                 257
         44        DO_ICALL                                         $13     
         45        FREE                                                     ~1
         46      > RETURN                                                   $13
   39    47    >   FREE                                                     ~1
         48      > RETURN                                                   <false>
         49*       FREE                                                     ~1
   41    50*     > RETURN                                                   null

End of function to_int

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.56 ms | 945 KiB | 26 Q