3v4l.org

run code in 500+ PHP versions simultaneously
<?php function absint( $maybeint ): int { if ( ! is_numeric( $maybeint ) ) { return 0; } if ( is_float( $maybeint ) ) { $value = abs( $maybeint ); if ( $value >= PHP_INT_MAX ) { return PHP_INT_MAX; } else { return (int) $value; } } // Convert numeric-string to int. $value = (int) $maybeint; // Special case for the one integer which cannot survive abs(). if ( PHP_INT_MIN === $value ) { return PHP_INT_MAX; } return abs( $value ); } $maybeints = array( 'positive max int times 2' => PHP_INT_MAX * 2, 'zero' => 0, 'string' => 'foo', 'negative 1 int' => -1, 'positive 1 int' => 1, 'negative min int' => PHP_INT_MIN, 'negative min int minus 1' => PHP_INT_MIN - 1, 'negative min int times 2' => PHP_INT_MIN * 2, 'positive max int' => PHP_INT_MAX, 'positive max int plus 1' => PHP_INT_MAX + 1, 'positive max int as string' => (string) PHP_INT_MAX, 'positive max int times 1000 as string' => (string) PHP_INT_MAX . '000', ); foreach ( $maybeints as $case => $maybeint ) { echo "# $case\n\n"; echo 'Input: '; var_dump( $maybeint ); echo 'Output: '; var_dump( absint( $maybeint ) ); echo "\n"; }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.33, 8.3.0 - 8.3.33, 8.4.1 - 8.4.24, 8.5.2 - 8.5.9
# positive max int times 2 Input: float(1.8446744073709552E+19) Output: int(9223372036854775807) # zero Input: int(0) Output: int(0) # string Input: string(3) "foo" Output: int(0) # negative 1 int Input: int(-1) Output: int(1) # positive 1 int Input: int(1) Output: int(1) # negative min int Input: int(-9223372036854775808) Output: int(9223372036854775807) # negative min int minus 1 Input: float(-9.223372036854776E+18) Output: int(9223372036854775807) # negative min int times 2 Input: float(-1.8446744073709552E+19) Output: int(9223372036854775807) # positive max int Input: int(9223372036854775807) Output: int(9223372036854775807) # positive max int plus 1 Input: float(9.223372036854776E+18) Output: int(9223372036854775807) # positive max int as string Input: string(19) "9223372036854775807" Output: int(9223372036854775807) # positive max int times 1000 as string Input: string(22) "9223372036854775807000" Output: int(9223372036854775807)
Output for 8.5.0 - 8.5.1
# positive max int times 2 Input: float(1.8446744073709552E+19) Output: int(9223372036854775807) # zero Input: int(0) Output: int(0) # string Input: string(3) "foo" Output: int(0) # negative 1 int Input: int(-1) Output: int(1) # positive 1 int Input: int(1) Output: int(1) # negative min int Input: int(-9223372036854775808) Output: int(9223372036854775807) # negative min int minus 1 Input: float(-9.223372036854776E+18) Output: int(9223372036854775807) # negative min int times 2 Input: float(-1.8446744073709552E+19) Output: int(9223372036854775807) # positive max int Input: int(9223372036854775807) Output: int(9223372036854775807) # positive max int plus 1 Input: float(9.223372036854776E+18) Output: int(9223372036854775807) # positive max int as string Input: string(19) "9223372036854775807" Output: int(9223372036854775807) # positive max int times 1000 as string Input: string(22) "9223372036854775807000" Output: Warning: The float-string "9223372036854775807000" is not representable as an int, cast occurred in /in/vuYa8 on line 18 int(9223372036854775807)
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
# positive max int times 2 Input: float(1.844674407371E+19) Output: int(9223372036854775807) # zero Input: int(0) Output: int(0) # string Input: string(3) "foo" Output: int(0) # negative 1 int Input: int(-1) Output: int(1) # positive 1 int Input: int(1) Output: int(1) # negative min int Input: int(-9223372036854775808) Output: int(9223372036854775807) # negative min int minus 1 Input: float(-9.2233720368548E+18) Output: int(9223372036854775807) # negative min int times 2 Input: float(-1.844674407371E+19) Output: int(9223372036854775807) # positive max int Input: int(9223372036854775807) Output: int(9223372036854775807) # positive max int plus 1 Input: float(9.2233720368548E+18) Output: int(9223372036854775807) # positive max int as string Input: string(19) "9223372036854775807" Output: int(9223372036854775807) # positive max int times 1000 as string Input: string(22) "9223372036854775807000" Output: int(9223372036854775807)
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Parse error: syntax error, unexpected ':', expecting '{' in /in/vuYa8 on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected ':', expecting '{' in /in/vuYa8 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/vuYa8 on line 3
Process exited with code 255.

preferences:
79.69 ms | 3084 KiB | 4 Q