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; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0090.00618.55
8.3.50.0120.01022.12
8.3.40.0040.01118.84
8.3.30.0100.00718.95
8.3.20.0030.00520.34
8.3.10.0060.00320.48
8.3.00.0040.00419.36
8.2.180.0120.00316.87
8.2.170.0040.01118.93
8.2.160.0100.00322.96
8.2.150.0120.00325.66
8.2.140.0030.00524.66
8.2.130.0000.00826.16
8.2.120.0000.00822.23
8.2.110.0070.00320.48
8.2.100.0090.00317.72
8.2.90.0000.00819.22
8.2.80.0040.00417.97
8.2.70.0080.00017.63
8.2.60.0050.00317.93
8.2.50.0030.00618.07
8.2.40.0000.01018.16
8.2.30.0060.00319.45
8.2.20.0050.00317.82
8.2.10.0040.00418.00
8.2.00.0040.00417.86
8.1.280.0070.01125.92
8.1.270.0070.00024.66
8.1.260.0060.00326.35
8.1.250.0030.00528.09
8.1.240.0090.00020.82
8.1.230.0040.00822.63
8.1.220.0040.00417.78
8.1.210.0040.00420.30
8.1.200.0070.00717.25
8.1.190.0040.00417.48
8.1.180.0000.00818.10
8.1.170.0080.00018.63
8.1.160.0000.00722.11
8.1.150.0070.00018.95
8.1.140.0000.00717.56
8.1.130.0070.00017.85
8.1.120.0040.00417.45
8.1.110.0050.00217.50
8.1.100.0050.00317.43
8.1.90.0000.00717.57
8.1.80.0000.00717.46
8.1.70.0030.00517.42
8.1.60.0000.00917.71
8.1.50.0040.00417.50
8.1.40.0000.00817.56
8.1.30.0050.00317.65
8.1.20.0040.00417.70
8.1.10.0040.00417.52
8.1.00.0000.00717.57
8.0.300.0090.00021.98
8.0.290.0040.00417.00
8.0.280.0000.00718.63
8.0.270.0090.00017.34
8.0.260.0030.00317.40
8.0.250.0000.00717.21
8.0.240.0000.00717.04
8.0.230.0030.00317.11
8.0.220.0030.00317.11
8.0.210.0000.00717.15
8.0.200.0040.00417.11
8.0.190.0000.00817.16
8.0.180.0040.00417.12
8.0.170.0040.00417.07
8.0.160.0040.00416.92
8.0.150.0030.00516.84
8.0.140.0000.00817.10
8.0.130.0060.00013.50
8.0.120.0040.00417.08
8.0.110.0040.00417.11
8.0.100.0040.00417.12
8.0.90.0070.00016.95
8.0.80.0000.01517.06
8.0.70.0040.00417.13
8.0.60.0040.00416.94
8.0.50.0030.00516.96
8.0.30.0080.01117.06
8.0.20.0110.00917.47
8.0.10.0000.00717.01
8.0.00.0150.00616.85
7.4.330.0000.00615.00
7.4.320.0000.00616.54
7.4.300.0040.00416.68
7.4.290.0030.00316.59
7.4.280.0000.00716.61
7.4.270.0080.00016.68
7.4.260.0070.00016.54
7.4.250.0040.00416.51
7.4.240.0030.00516.60
7.4.230.0070.00016.48
7.4.220.0080.01116.61
7.4.210.0090.00916.57
7.4.200.0090.00016.57
7.4.160.0080.00816.58
7.4.150.0150.01217.40
7.4.140.0100.00717.86
7.4.130.0080.00916.52
7.4.120.0110.00716.68
7.4.110.0150.00716.61
7.4.100.0060.01116.54
7.4.90.0130.00516.54
7.4.80.0090.01619.04
7.4.70.0060.01116.63
7.4.60.0060.01316.57
7.4.50.0000.00516.71
7.4.40.0140.00516.63
7.4.30.0030.01316.75
7.4.00.0080.00914.88
7.3.330.0000.00613.34
7.3.320.0050.00013.54
7.3.310.0040.00416.57
7.3.300.0070.00016.41
7.3.290.0090.00616.54
7.3.280.0070.01216.57
7.3.270.0160.00917.40
7.3.260.0060.01216.46
7.3.250.0090.00916.40
7.3.240.0080.00816.45
7.3.230.0060.01216.41
7.3.210.0090.00916.55
7.3.200.0120.00419.39
7.3.190.0000.01616.34
7.3.180.0110.01116.46
7.3.170.0090.00916.62
7.3.160.0120.00316.50
7.3.120.0100.00714.77
7.3.110.0080.00915.00
7.3.100.0050.00614.91
7.3.90.0080.00715.01
7.3.80.0050.00814.95
7.3.70.0040.00914.91
7.3.60.0090.00914.98
7.3.50.0020.01015.02
7.3.40.0120.00314.86
7.3.30.0060.00614.95
7.3.20.0080.00716.79
7.3.10.0050.00616.63
7.3.00.0070.00516.66
7.2.330.0060.01116.55
7.2.320.0120.00616.60
7.2.310.0070.01116.61
7.2.300.0090.00916.63
7.2.290.0030.02016.79
7.2.250.0050.01314.94
7.2.240.0060.01115.05
7.2.230.0020.00715.14
7.2.220.0050.00815.25
7.2.210.0100.00315.18
7.2.200.0080.00315.04
7.2.190.0050.00915.07
7.2.180.0020.01014.97
7.2.170.0070.00715.13
7.2.60.0070.00717.09
7.2.00.0000.01219.54
7.1.330.0070.00615.97
7.1.320.0040.00715.99
7.1.310.0060.00615.93
7.1.300.0070.01015.87
7.1.290.0070.00515.72
7.1.280.0020.01115.87
7.1.270.0070.00715.90
7.1.260.0080.00615.81
7.1.200.0030.00915.93
7.1.100.0000.01318.09
7.1.70.0130.00616.99
7.1.60.0040.01219.32
7.1.50.0090.01516.99
7.1.00.0070.07322.33
7.0.200.0240.00514.88
7.0.140.0030.07322.09
7.0.110.0170.05319.98
7.0.100.0200.04319.98
7.0.90.0400.03720.10
7.0.80.0600.04319.95
7.0.70.0330.04019.87
7.0.60.0330.03719.96
7.0.50.0100.04719.97
7.0.40.0070.06019.74
7.0.30.0000.06319.84
7.0.20.0030.03719.79
7.0.10.0000.04019.70
7.0.00.0100.07719.65
5.6.280.0070.07021.15
5.6.260.0070.04020.63
5.6.250.0030.04720.65
5.6.240.0030.04020.54
5.6.230.0100.06320.60
5.6.220.0100.03320.64
5.6.210.0030.04320.55
5.6.200.0100.06020.60
5.6.190.0100.03320.83
5.6.180.0100.04020.59
5.6.170.0030.04020.54
5.6.160.0000.06020.57
5.6.150.0030.04320.51
5.6.140.0030.04020.63
5.6.130.0100.03320.63
5.6.120.0100.03320.63
5.6.110.0130.06720.43
5.6.100.0030.04020.55
5.6.90.0070.05320.80
5.6.80.0070.07319.91
5.6.70.0030.04319.90
5.6.60.0000.04319.97
5.6.50.0070.03019.93
5.6.40.0030.04719.89
5.6.30.0030.05719.95
5.6.20.0100.08020.04
5.6.10.0030.08719.81
5.6.00.0130.07319.89
5.5.380.0030.04720.52
5.5.370.0070.05720.44
5.5.360.0070.04320.54
5.5.350.0000.04720.38
5.5.340.0070.03720.73
5.5.330.0000.04320.87
5.5.320.0100.07720.86
5.5.310.0030.04020.93
5.5.300.0100.03720.96
5.5.290.0070.04020.95
5.5.280.0030.04321.00
5.5.270.0100.03720.86
5.5.260.0100.04020.95
5.5.250.0000.04020.61
5.5.240.0030.04020.14
5.5.230.0030.04720.32
5.5.220.0000.04020.30
5.5.210.0000.07720.09
5.5.200.0030.06320.13
5.5.190.0130.08020.13
5.5.180.0070.07720.27
5.5.160.0100.08320.05
5.5.150.0070.07720.33
5.5.140.0130.07720.02
5.5.130.0070.07720.16
5.5.120.0130.07020.13
5.5.110.0130.04720.26
5.5.100.0100.07320.08
5.5.90.0100.07720.01
5.5.80.0070.07720.07
5.5.70.0100.07320.14
5.5.60.0170.04720.16
5.5.50.0070.08020.00
5.5.40.0070.08020.06
5.5.30.0100.07320.18
5.5.20.0070.08020.12
5.5.10.0170.06720.02
5.5.00.0100.07720.07
5.4.450.0030.04019.64
5.4.440.0000.04319.27
5.4.430.0030.04019.55
5.4.420.0070.03719.50
5.4.410.0000.04319.42
5.4.400.0070.04719.27
5.4.390.0100.02718.99
5.4.380.0000.04019.01
5.4.370.0030.04319.17
5.4.360.0070.03319.04
5.4.350.0130.07019.09
5.4.340.0000.06319.27
5.4.320.0030.07319.32
5.4.310.0100.07019.01
5.4.300.0200.04719.24
5.4.290.0170.07019.23
5.4.280.0000.08019.09
5.4.270.0100.08019.24
5.4.260.0070.08018.94
5.4.250.0100.06019.17
5.4.240.0170.06718.98
5.4.230.0130.06319.25
5.4.220.0070.07719.08
5.4.210.0000.08719.00
5.4.200.0100.07719.34
5.4.190.0030.06319.19
5.4.180.0130.07019.32
5.4.170.0170.06718.97
5.4.160.0100.06019.12
5.4.150.0070.07719.00
5.4.140.0000.05316.28
5.4.130.0030.08016.56
5.4.120.0170.05716.55
5.4.110.0100.07316.56
5.4.100.0130.06016.52
5.4.90.0200.06716.55
5.4.80.0130.04716.45
5.4.70.0130.06716.53
5.4.60.0200.05716.34
5.4.50.0170.06316.34
5.4.40.0100.07316.45
5.4.30.0030.07716.42
5.4.20.0100.07316.54
5.4.10.0130.07316.43
5.4.00.0100.07016.02
5.3.290.0070.08314.76
5.3.280.0000.08014.69
5.3.270.0070.08014.69
5.3.260.0170.06714.69
5.3.250.0100.06714.69
5.3.240.0130.06714.69
5.3.230.0130.07014.69
5.3.220.0100.07314.69
5.3.210.0100.07314.69
5.3.200.0070.08014.69
5.3.190.0100.05714.69
5.3.180.0030.08014.69
5.3.170.0130.06714.69
5.3.160.0030.08014.69
5.3.150.0070.07714.69
5.3.140.0100.07014.69
5.3.130.0100.07014.69
5.3.120.0070.06714.69
5.3.110.0030.07314.69
5.3.100.0070.04314.69
5.3.90.0100.07314.69
5.3.80.0000.07314.69
5.3.70.0100.06014.69
5.3.60.0170.06714.69
5.3.50.0070.07014.69
5.3.40.0130.06314.69
5.3.30.0100.05014.69
5.3.20.0030.08014.69
5.3.10.0030.07314.69
5.3.00.0070.07714.69
5.2.170.0130.05014.69
5.2.160.0000.06714.69
5.2.150.0170.05314.69
5.2.140.0030.06314.69
5.2.130.0070.05714.69
5.2.120.0100.03714.69
5.2.110.0100.05714.69
5.2.100.0100.05714.69
5.2.90.0070.05014.69
5.2.80.0070.06314.69
5.2.70.0100.05714.69
5.2.60.0130.05014.69
5.2.50.0100.06014.69
5.2.40.0070.06314.69
5.2.30.0100.06014.69
5.2.20.0200.04714.69
5.2.10.0030.05314.69
5.2.00.0070.06314.69
5.1.60.0070.04314.69
5.1.50.0030.05014.69
5.1.40.0030.05714.69
5.1.30.0130.04314.69
5.1.20.0070.05014.69
5.1.10.0000.06014.69
5.1.00.0030.05714.69
5.0.50.0070.04014.69
5.0.40.0070.04014.69
5.0.30.0000.04714.69
5.0.20.0100.03314.69
5.0.10.0000.04014.69
5.0.00.0030.07014.69
4.4.90.0070.03314.69
4.4.80.0030.03014.69
4.4.70.0130.02714.69
4.4.60.0070.03014.69
4.4.50.0000.04014.69
4.4.40.0070.05014.69
4.4.30.0030.03714.69
4.4.20.0030.03314.69
4.4.10.0070.03314.69
4.4.00.0070.04014.69
4.3.110.0030.03314.69
4.3.100.0030.03314.69
4.3.90.0000.03714.69
4.3.80.0030.05014.69
4.3.70.0070.03014.69
4.3.60.0000.02014.69
4.3.50.0030.03314.69
4.3.40.0000.05014.69
4.3.30.0000.03714.69
4.3.20.0000.04014.69
4.3.10.0000.03714.69
4.3.00.0000.03714.69

preferences:
47.08 ms | 401 KiB | 5 Q