3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo "<pre>"; 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([])); echo "</pre>"; /** * 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.18
8.3.50.0130.00821.97
8.3.40.0040.01118.67
8.3.30.0110.00718.79
8.3.20.0030.00519.95
8.3.10.0040.00420.61
8.3.00.0030.00519.38
8.2.180.0070.00718.54
8.2.170.0090.00622.96
8.2.160.0130.00320.38
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0080.00026.16
8.2.120.0070.00420.99
8.2.110.0060.00320.35
8.2.100.0070.00417.84
8.2.90.0000.00819.01
8.2.80.0040.00417.97
8.2.70.0050.00517.63
8.2.60.0000.01017.79
8.2.50.0080.00018.07
8.2.40.0030.00518.21
8.2.30.0040.00419.27
8.2.20.0040.00417.72
8.2.10.0050.00318.08
8.2.00.0000.00817.75
8.1.280.0140.00425.92
8.1.270.0040.00424.02
8.1.260.0040.00426.35
8.1.250.0050.00328.09
8.1.240.0070.00323.86
8.1.230.0040.00723.73
8.1.220.0050.00317.74
8.1.210.0000.00818.77
8.1.200.0040.00417.35
8.1.190.0040.00417.23
8.1.180.0050.00318.10
8.1.170.0080.00018.59
8.1.160.0000.00722.01
8.1.150.0040.00418.93
8.1.140.0000.00717.53
8.1.130.0030.00317.91
8.1.120.0070.00017.40
8.1.110.0050.00217.51
8.1.100.0030.00517.43
8.1.90.0040.00417.44
8.1.80.0040.00417.49
8.1.70.0030.00317.38
8.1.60.0000.00917.59
8.1.50.0060.00317.58
8.1.40.0000.00817.56
8.1.30.0080.00017.66
8.1.20.0040.00417.62
8.1.10.0000.00817.60
8.1.00.0000.00817.45
8.0.300.0040.00418.77
8.0.290.0030.00517.00
8.0.280.0000.00718.67
8.0.270.0030.00717.36
8.0.260.0030.00317.40
8.0.250.0070.00017.02
8.0.240.0000.00717.08
8.0.230.0070.00017.02
8.0.220.0000.00717.07
8.0.210.0030.00317.09
8.0.200.0080.00017.03
8.0.190.0040.00417.14
8.0.180.0040.00416.99
8.0.170.0050.00317.06
8.0.160.0050.00317.08
8.0.150.0040.00416.93
8.0.140.0050.00216.91
8.0.130.0000.00613.45
8.0.120.0030.00617.00
8.0.110.0030.00517.01
8.0.100.0040.00416.90
8.0.90.0040.00417.00
8.0.80.0030.01116.94
8.0.70.0040.00416.92
8.0.60.0000.00717.07
8.0.50.0000.00717.01
8.0.30.0090.01117.06
8.0.20.0130.00517.40
8.0.10.0080.00017.08
8.0.00.0070.01116.74
7.4.330.0000.00515.02
7.4.320.0000.00616.54
7.4.300.0030.00316.52
7.4.290.0070.00016.62
7.4.280.0040.00416.52
7.4.270.0030.00316.41
7.4.260.0030.00316.53
7.4.250.0000.00816.46
7.4.240.0030.00316.56
7.4.230.0030.00316.64
7.4.220.0070.01016.50
7.4.210.0130.00616.54
7.4.200.0030.00616.38
7.4.160.0050.01116.80
7.4.150.0170.00917.40
7.4.140.0130.00717.86
7.4.130.0090.01016.55
7.4.120.0060.01116.55
7.4.110.0110.00716.75
7.4.100.0090.00916.42
7.4.90.0100.01416.48
7.4.80.0030.01519.03
7.4.70.0070.01016.61
7.4.60.0090.01216.69
7.4.50.0060.00316.65
7.4.40.0100.00716.66
7.4.30.0040.01216.36
7.4.10.0070.01014.88
7.4.00.0090.00714.93
7.3.330.0000.00613.56
7.3.320.0000.00513.52
7.3.310.0070.00016.66
7.3.300.0040.00416.41
7.3.290.0000.01416.47
7.3.280.0100.00816.55
7.3.270.0210.00317.40
7.3.260.0160.00316.71
7.3.250.0150.00516.48
7.3.240.0140.00316.65
7.3.230.0140.00316.62
7.3.210.0090.01216.43
7.3.200.0060.01219.39
7.3.190.0060.01116.57
7.3.180.0030.01316.61
7.3.170.0100.00716.73
7.3.160.0090.00616.71
7.3.130.0060.01214.73
7.3.120.0090.00915.10
7.3.110.0100.00815.06
7.3.100.0080.00815.03
7.3.90.0030.01014.67
7.3.80.0010.01114.98
7.3.70.0020.01214.63
7.3.60.0030.00914.65
7.3.50.0050.00714.74
7.3.40.0080.00514.74
7.3.30.0050.01014.93
7.3.20.0110.00316.56
7.3.10.0090.00416.58
7.3.00.0090.00716.71
7.2.330.0130.00416.92
7.2.320.0130.00716.66
7.2.310.0030.01516.83
7.2.300.0120.00916.63
7.2.290.0030.01416.74
7.2.260.0070.01015.11
7.2.250.0100.00915.24
7.2.240.0090.00815.11
7.2.230.0050.00815.04
7.2.220.0050.00715.05
7.2.210.0060.00515.03
7.2.200.0050.00515.05
7.2.190.0050.01015.16
7.2.180.0050.01014.97
7.2.170.0050.00915.06
7.2.160.0050.01314.99
7.2.150.0070.00716.70
7.2.140.0080.00816.87
7.2.130.0070.00816.99
7.2.120.0060.00916.86
7.2.110.0050.00916.92
7.2.100.0070.01016.81
7.2.90.0040.01216.97
7.2.80.0130.00416.93
7.2.70.0120.00716.82
7.2.60.0100.00416.91
7.2.50.0060.01016.90
7.2.40.0070.00616.77
7.2.30.0050.01016.93
7.2.20.0030.01416.81
7.2.10.0080.01016.69
7.2.00.0070.00817.54
7.1.330.0080.00815.60
7.1.320.0050.01115.72
7.1.310.0070.00715.65
7.1.300.0090.00515.58
7.1.290.0070.00715.80
7.1.280.0080.00415.78
7.1.270.0050.00915.61
7.1.260.0080.00615.68
7.1.250.0080.00515.74
7.1.240.0070.00915.78
7.1.230.0070.00715.74
7.1.220.0050.00715.82
7.1.210.0060.00215.61
7.1.200.0070.00715.67
7.1.190.0050.00515.80
7.1.180.0080.00215.77
7.1.170.0050.00815.40
7.1.160.0030.00815.70
7.1.150.0040.00915.80
7.1.140.0030.00816.00
7.1.130.0070.00815.84
7.1.120.0060.01115.87
7.1.110.0030.01315.80
7.1.100.0040.01016.54
7.1.90.0120.00315.89
7.1.80.0080.00815.64
7.1.70.0050.00916.30
7.1.60.0050.00816.84
7.1.50.0050.00716.11
7.1.40.0050.00815.66
7.1.30.0070.00715.70
7.1.20.0030.01015.89
7.1.10.0020.01315.82
7.1.00.0040.03318.04
7.0.330.0060.00715.44
7.0.320.0040.00815.29
7.0.310.0050.00915.43
7.0.300.0020.01315.42
7.0.290.0040.00615.31
7.0.280.0020.01415.35
7.0.270.0060.00615.47
7.0.260.0100.00615.55
7.0.250.0050.01215.32
7.0.240.0070.00515.40
7.0.230.0060.00715.60
7.0.220.0040.01015.47
7.0.210.0040.00915.39
7.0.200.0030.00915.81
7.0.190.0020.00715.29
7.0.180.0020.01115.46
7.0.170.0040.00715.44
7.0.160.0030.01015.47
7.0.150.0030.01115.15
7.0.140.0060.02817.75
7.0.130.0030.01215.39
7.0.120.0070.00415.58
7.0.110.0050.00815.30
7.0.100.0060.00315.47
7.0.90.0050.00615.39
7.0.80.0000.01315.43
7.0.70.0040.00815.32
7.0.60.0070.01416.89
7.0.50.0060.02516.20
7.0.40.0070.02415.69
7.0.30.0160.02915.68
7.0.20.0080.02015.62
7.0.10.0060.01915.70
7.0.00.0030.02115.76
5.6.400.0020.01014.49
5.6.390.0020.01214.15
5.6.380.0030.00914.52
5.6.370.0050.01114.26
5.6.360.0050.00714.27
5.6.350.0080.00814.30
5.6.340.0050.00914.22
5.6.330.0080.00614.38
5.6.320.0130.00314.38
5.6.310.0060.00814.20
5.6.300.0050.01014.46
5.6.290.0080.00914.27
5.6.280.0060.02516.64
5.6.270.0100.00514.18
5.6.260.0020.01114.33
5.6.250.0030.01014.30
5.6.240.0030.01014.27
5.6.230.0070.00614.52
5.6.220.0020.01214.38
5.6.210.0140.02416.45
5.6.200.0080.03315.74
5.6.190.0060.02816.38
5.6.180.0210.02616.46
5.6.170.0170.02916.31
5.6.160.0020.02316.54
5.6.150.0010.03015.73
5.6.140.0060.03515.53
5.6.130.0070.02715.81
5.6.120.0090.01416.58
5.6.110.0060.03416.71
5.6.100.0120.01616.47
5.6.90.0080.03116.51
5.6.80.0060.03416.21
5.6.70.0060.00914.34
5.6.60.0040.01014.31
5.6.50.0070.00814.41
5.6.40.0060.00814.21
5.6.30.0030.01214.12
5.6.20.0080.00714.20
5.6.10.0050.00714.22
5.6.00.0070.00614.20
5.5.380.0050.01014.37
5.5.370.0030.00614.18
5.5.360.0070.00514.29
5.5.350.0120.03016.45
5.5.340.0100.02915.65
5.5.330.0060.02516.33
5.5.320.0100.02216.26
5.5.310.0060.02416.20
5.5.300.0060.03315.56
5.5.290.0040.01815.61
5.5.280.0080.02216.46
5.5.270.0060.03716.37
5.5.260.0060.02116.57
5.5.250.0070.03516.35
5.5.240.0110.02716.15
5.5.230.0060.00614.39
5.5.220.0090.00614.26
5.5.210.0030.00814.05
5.5.200.0080.00714.12
5.5.190.0040.01114.37
5.5.180.0070.00914.35
5.5.170.0070.00614.13
5.5.160.0080.00614.34
5.5.150.0060.00914.23
5.5.140.0110.00414.29
5.5.130.0120.00314.40
5.5.120.0080.00714.12
5.5.110.0070.00414.11
5.5.100.0090.00414.38
5.5.90.0050.00714.11
5.5.80.0090.00513.78
5.5.70.0050.01114.12
5.5.60.0060.00614.13
5.5.50.0070.00814.20
5.5.40.0050.00514.27
5.5.30.0050.00714.13
5.5.20.0050.01014.08
5.5.10.0030.01114.31
5.5.00.0060.00614.16
5.4.450.0030.02014.53
5.4.440.0340.02514.56
5.4.430.0330.01914.53
5.4.420.0290.00214.57
5.4.410.0220.00614.60
5.4.400.0250.00314.47
5.4.390.0220.00514.47
5.4.380.0140.02214.41
5.4.370.0120.02614.38
5.4.360.0170.02214.40
5.4.350.0070.02914.38
5.4.340.0110.02514.33
5.4.330.0050.00412.19
5.4.320.0020.02012.30
5.4.310.0040.01912.30
5.4.300.0040.01612.30
5.4.290.0070.01312.30
5.4.280.0060.02012.27
5.4.270.0070.01512.27
5.4.260.0060.01712.27
5.4.250.0040.01612.27
5.4.240.0060.01612.27
5.4.230.0030.02012.27
5.4.220.0060.01512.27
5.4.210.0030.01612.27
5.4.200.0020.01912.27
5.4.190.0050.01412.27
5.4.180.0060.01412.26
5.4.170.0050.01812.27
5.4.160.0040.01712.26
5.4.150.0040.01712.26
5.4.140.0050.01912.16
5.4.130.0030.01612.15
5.4.120.0040.01612.14
5.4.110.0060.01612.14
5.4.100.0080.01312.14
5.4.90.0050.01712.14
5.4.80.0030.01912.14
5.4.70.0030.01712.14
5.4.60.0050.01512.14
5.4.50.0080.01212.13
5.4.40.0050.01512.13
5.4.30.0040.01612.13
5.4.20.0040.01812.13
5.4.10.0040.01712.13
5.4.00.0040.01711.96
5.3.290.0060.03912.80
5.3.280.0060.03712.71
5.3.270.0050.03912.72
5.3.260.0100.03812.72
5.3.250.0050.03712.72
5.3.240.0070.03912.72
5.3.230.0070.03612.71
5.3.220.0010.04512.68
5.3.210.0100.04112.68
5.3.200.0050.04612.68
5.3.190.0100.03812.68
5.3.180.0060.03612.67
5.3.170.0090.03412.67
5.3.160.0100.04012.67
5.3.150.0080.03612.67
5.3.140.0030.03912.66
5.3.130.0070.03812.66
5.3.120.0050.03912.66
5.3.110.0080.03512.66
5.3.100.0070.03512.12
5.3.90.0060.03812.08
5.3.80.0090.03612.07
5.3.70.0040.03612.07
5.3.60.0100.03712.07
5.3.50.0060.04012.00
5.3.40.0080.03412.00
5.3.30.0080.03211.96
5.3.20.0050.03611.75
5.3.10.0050.04011.71
5.3.00.0080.04111.70
5.2.170.0070.0309.20
5.2.160.0040.0349.21
5.2.150.0020.0349.20
5.2.140.0090.0349.20
5.2.130.0050.0319.16
5.2.120.0050.0289.16
5.2.110.0030.0319.17
5.2.100.0060.0279.16
5.2.90.0050.0289.16
5.2.80.0020.0339.16
5.2.70.0050.0299.15
5.2.60.0020.0319.11
5.2.50.0030.0319.08
5.2.40.0040.0299.06
5.2.30.0060.0289.04
5.2.20.0050.0299.02
5.2.10.0050.0288.95
5.2.00.0030.0318.80
5.1.60.0020.0268.09
5.1.50.0050.0248.09
5.1.40.0040.0248.06
5.1.30.0030.0278.41
5.1.20.0050.0328.44
5.1.10.0070.0278.16
5.1.00.0040.0258.16
5.0.50.0030.0246.64
5.0.40.0010.0216.51
5.0.30.0040.0306.31
5.0.20.0040.0196.31
5.0.10.0040.0266.29
5.0.00.0020.0316.28
4.4.90.0010.0174.78
4.4.80.0030.0154.75
4.4.70.0020.0164.76
4.4.60.0010.0174.75
4.4.50.0020.0164.77
4.4.40.0030.0274.70
4.4.30.0020.0164.76
4.4.20.0040.0154.84
4.4.10.0020.0164.85
4.4.00.0040.0264.76
4.3.110.0020.0164.67
4.3.100.0030.0154.66
4.3.90.0030.0144.64
4.3.80.0040.0234.59
4.3.70.0040.0134.63
4.3.60.0020.0154.63
4.3.50.0020.0174.63
4.3.40.0030.0234.54
4.3.30.0020.0163.33
4.3.20.0020.0173.32
4.3.10.0010.0173.27
4.3.00.0000.01715.60

preferences:
56.68 ms | 401 KiB | 5 Q