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")); var_dump(to_int("31e+7")); echo "Should fail" . PHP_EOL; var_dump(to_int(1.5)); var_dump(to_int("1.5")); var_dump(to_int("75e-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.0180.00318.43
8.3.50.0160.00522.00
8.3.40.0110.00418.80
8.3.30.0090.00919.00
8.3.20.0040.00420.34
8.3.10.0000.00820.56
8.3.00.0030.00617.63
8.2.180.0180.00016.63
8.2.170.0120.00422.96
8.2.160.0100.00720.35
8.2.150.0000.00724.18
8.2.140.0000.00824.66
8.2.130.0080.00026.16
8.2.120.0040.00420.91
8.2.110.0030.00720.45
8.2.100.0090.00319.64
8.2.90.0080.00019.17
8.2.80.0060.00317.97
8.2.70.0050.00317.63
8.2.60.0060.00317.80
8.2.50.0050.00518.07
8.2.40.0050.00518.32
8.2.30.0070.00019.32
8.2.20.0050.00317.87
8.2.10.0000.00818.01
8.2.00.0000.00717.71
8.1.280.0140.00325.92
8.1.270.0070.00422.22
8.1.260.0080.00026.35
8.1.250.0070.00028.09
8.1.240.0090.00020.94
8.1.230.0000.01219.13
8.1.220.0040.00417.89
8.1.210.0030.00618.77
8.1.200.0000.00917.48
8.1.190.0000.00917.35
8.1.180.0050.00318.10
8.1.170.0050.00318.75
8.1.160.0040.00422.13
8.1.150.0070.00018.87
8.1.140.0040.00417.56
8.1.130.0070.00017.89
8.1.120.0030.00417.53
8.1.110.0020.00517.41
8.1.100.0040.00417.49
8.1.90.0040.00417.48
8.1.80.0040.00417.51
8.1.70.0000.00717.50
8.1.60.0060.00317.65
8.1.50.0000.00917.59
8.1.40.0050.00317.62
8.1.30.0080.00017.53
8.1.20.0050.00317.70
8.1.10.0040.00417.65
8.1.00.0030.00517.50
8.0.300.0070.00018.77
8.0.290.0020.00516.88
8.0.280.0030.00318.48
8.0.270.0100.00017.43
8.0.260.0060.00017.30
8.0.250.0030.00317.15
8.0.240.0040.00417.14
8.0.230.0000.00717.09
8.0.220.0050.00217.02
8.0.210.0030.00316.99
8.0.200.0080.00017.01
8.0.190.0090.00017.09
8.0.180.0000.00816.95
8.0.170.0080.00017.10
8.0.160.0000.00717.11
8.0.150.0030.00617.07
8.0.140.0000.00717.03
8.0.130.0060.00013.51
8.0.120.0000.00817.05
8.0.110.0000.00716.98
8.0.100.0030.00616.96
8.0.90.0000.00717.13
8.0.80.0040.01017.13
8.0.70.0050.00316.95
8.0.60.0030.00617.07
8.0.50.0000.00816.95
8.0.30.0090.01017.28
8.0.20.0090.01117.40
8.0.10.0000.00717.02
8.0.00.0130.01216.83
7.4.330.0060.00015.14
7.4.320.0000.00616.71
7.4.300.0060.00016.69
7.4.290.0030.00316.56
7.4.280.0080.00016.66
7.4.270.0050.00316.68
7.4.260.0030.00416.70
7.4.250.0060.00316.63
7.4.240.0070.00016.69
7.4.230.0070.00016.46
7.4.220.0130.00916.71
7.4.210.0090.00616.78
7.4.200.0000.00916.56
7.4.160.0080.00816.59
7.4.150.0160.01117.40
7.4.140.0140.00417.86
7.4.130.0110.00616.67
7.4.120.0090.00816.59
7.4.110.0090.01416.60
7.4.100.0150.00816.46
7.4.90.0150.00416.62
7.4.80.0140.00519.04
7.4.70.0070.01016.75
7.4.60.0110.00716.59
7.4.50.0030.00616.70
7.4.40.0060.01216.64
7.4.30.0110.00716.81
7.4.10.0030.01515.14
7.4.00.0100.00614.80
7.3.330.0030.00313.56
7.3.320.0000.00513.54
7.3.310.0000.00816.45
7.3.300.0070.00016.41
7.3.290.0070.00716.63
7.3.280.0070.01116.54
7.3.270.0080.01817.40
7.3.260.0110.00716.73
7.3.250.0100.00616.66
7.3.240.0060.01216.41
7.3.230.0150.00216.52
7.3.210.0060.01216.49
7.3.200.0050.01119.39
7.3.190.0070.01016.67
7.3.180.0080.00816.45
7.3.170.0060.01016.46
7.3.160.0040.01216.68
7.3.130.0060.01214.93
7.3.120.0110.00614.88
7.3.110.0090.01014.82
7.3.100.0090.00614.83
7.3.90.0050.00815.06
7.3.80.0060.00515.02
7.3.70.0080.00514.79
7.3.60.0040.01114.89
7.3.50.0070.00614.84
7.3.40.0020.01114.90
7.3.30.0060.00814.70
7.3.20.0020.01116.64
7.3.10.0050.00716.74
7.3.00.0030.00816.69
7.2.330.0090.01216.73
7.2.320.0090.00916.57
7.2.310.0030.01316.64
7.2.300.0100.00716.87
7.2.290.0060.01116.86
7.2.260.0060.00915.29
7.2.250.0070.01115.28
7.2.240.0040.01315.30
7.2.230.0040.01115.11
7.2.220.0080.00914.89
7.2.210.0060.00515.11
7.2.200.0060.00814.99
7.2.190.0040.00915.11
7.2.180.0020.00814.93
7.2.170.0090.00715.09
7.2.160.0050.00715.22
7.2.150.0020.01116.89
7.2.140.0050.01417.07
7.2.130.0030.01217.01
7.2.120.0030.01017.04
7.2.110.0050.00816.82
7.2.100.0070.00716.86
7.2.90.0040.01116.88
7.2.80.0020.01116.89
7.2.70.0070.00617.05
7.2.60.0030.00617.01
7.2.50.0100.00216.79
7.2.40.0030.00916.87
7.2.30.0050.01017.07
7.2.20.0120.00516.94
7.2.10.0050.00517.00
7.2.00.0070.00516.79
7.1.330.0060.00715.80
7.1.320.0100.00615.82
7.1.310.0050.00715.93
7.1.300.0100.00215.79
7.1.290.0040.00815.73
7.1.280.0080.00515.65
7.1.270.0070.00515.74
7.1.260.0000.01315.75
7.1.250.0030.00915.83
7.1.240.0060.00915.88
7.1.230.0100.00315.79
7.1.220.0040.00815.86
7.1.210.0040.00415.86
7.1.200.0110.00315.89
7.1.190.0040.01115.92
7.1.180.0070.00715.84
7.1.170.0000.01415.90
7.1.160.0070.00715.96
7.1.150.0070.00415.57
7.1.140.0090.00916.01
7.1.130.0060.00915.93
7.1.120.0030.01015.95
7.1.110.0090.00315.67
7.1.100.0060.00615.64
7.1.90.0040.01116.00
7.1.80.0030.01015.85
7.1.70.0030.00716.55
7.1.60.0060.00817.46
7.1.50.0030.01616.34
7.1.40.0070.00715.60
7.1.30.0060.00615.85
7.1.20.0000.00815.61
7.1.10.0080.00816.00
7.1.00.0030.04819.01
7.0.330.0030.00715.43
7.0.320.0030.01315.45
7.0.310.0070.00715.46
7.0.300.0070.00715.57
7.0.290.0030.00715.49
7.0.280.0050.00515.39
7.0.270.0000.01615.30
7.0.260.0040.00415.59
7.0.250.0030.01015.46
7.0.240.0040.00715.42
7.0.230.0090.00615.64
7.0.220.0040.00715.50
7.0.210.0030.01015.44
7.0.200.0040.00816.09
7.0.190.0070.00715.52
7.0.180.0040.00815.26
7.0.170.0040.01115.49
7.0.160.0030.00615.32
7.0.150.0050.00315.31
7.0.140.0050.04118.63
7.0.130.0110.00015.52
7.0.120.0060.00915.42
7.0.110.0040.00715.38
7.0.100.0040.01115.45
7.0.90.0080.00815.49
7.0.80.0030.01015.56
7.0.70.0060.00615.63
7.0.60.0080.02917.77
7.0.50.0050.03616.71
7.0.40.0100.03716.81
7.0.30.0120.04916.83
7.0.20.0130.04616.81
7.0.10.0080.03016.78
7.0.00.0050.05216.90
5.6.400.0060.00914.13
5.6.390.0070.00714.56
5.6.380.0040.00814.39
5.6.370.0110.00314.61
5.6.360.0000.01514.25
5.6.350.0040.00714.48
5.6.340.0120.00014.60
5.6.330.0070.01014.36
5.6.320.0060.00913.92
5.6.310.0030.01014.20
5.6.300.0070.00714.29
5.6.290.0030.01314.26
5.6.280.0050.04117.72
5.6.270.0060.00914.58
5.6.260.0070.00714.57
5.6.250.0090.00614.24
5.6.240.0070.01114.63
5.6.230.0030.01014.33
5.6.220.0070.00314.27
5.6.210.0030.02917.41
5.6.200.0020.03916.43
5.6.190.0080.04917.51
5.6.180.0120.02817.57
5.6.170.0270.03817.40
5.6.160.0030.04217.53
5.6.150.0080.03916.21
5.6.140.0050.03116.22
5.6.130.0090.03916.33
5.6.120.0100.04117.60
5.6.110.0020.05317.69
5.6.100.0020.04517.65
5.6.90.0080.03717.50
5.6.80.0100.02617.45
5.6.70.0040.00414.13
5.6.60.0040.00414.34
5.6.50.0100.00314.25
5.6.40.0070.00714.55
5.6.30.0030.01014.36
5.6.20.0070.00714.45
5.6.10.0100.00714.34
5.6.00.0090.00614.46
5.5.380.0000.01114.10
5.5.370.0000.01314.47
5.5.360.0030.00714.14
5.5.350.0120.04317.34
5.5.340.0020.02816.26
5.5.330.0090.04317.41
5.5.320.0230.04517.44
5.5.310.0080.03917.34
5.5.300.0080.02316.28
5.5.290.0090.04116.12
5.5.280.0160.02017.49
5.5.270.0100.03817.57
5.5.260.0080.04017.66
5.5.250.0110.03817.41
5.5.240.0120.03217.29
5.5.230.0110.00414.25
5.5.220.0040.00414.11
5.5.210.0040.01114.44
5.5.200.0030.01014.54
5.5.190.0040.01414.38
5.5.180.0050.01414.05
5.5.170.0100.00014.00
5.5.160.0030.01014.29
5.5.150.0060.00614.11
5.5.140.0070.01114.28
5.5.130.0060.01214.28
5.5.120.0000.01314.35
5.5.110.0080.00014.20
5.5.100.0070.00713.96
5.5.90.0100.00314.48
5.5.80.0060.01014.36
5.5.70.0090.00314.11
5.5.60.0040.01414.36
5.5.50.0030.01314.37
5.5.40.0030.01214.21
5.5.30.0050.00514.14
5.5.20.0030.01013.95
5.5.10.0150.00014.17
5.5.00.0000.00914.35
5.4.450.0120.02016.16
5.4.440.0570.02616.21
5.4.430.0500.03016.13
5.4.420.0370.00016.10
5.4.410.0360.00116.02
5.4.400.0380.00115.91
5.4.390.0350.00215.96
5.4.380.0120.03215.70
5.4.370.0170.03015.75
5.4.360.0220.02515.77
5.4.350.0160.03215.70
5.4.340.0280.02515.77
5.4.330.0040.01112.78
5.4.320.0020.02812.65
5.4.310.0050.02112.65
5.4.300.0070.02312.65
5.4.290.0070.02512.65
5.4.280.0050.02212.60
5.4.270.0020.02112.60
5.4.260.0060.02512.60
5.4.250.0080.02012.60
5.4.240.0040.02212.60
5.4.230.0060.02312.60
5.4.220.0080.02112.60
5.4.210.0020.02812.60
5.4.200.0070.02012.60
5.4.190.0060.02012.59
5.4.180.0080.02412.60
5.4.170.0040.02012.60
5.4.160.0020.02512.59
5.4.150.0050.02112.60
5.4.140.0040.02312.44
5.4.130.0050.02712.43
5.4.120.0060.01912.41
5.4.110.0060.01912.41
5.4.100.0030.02112.41
5.4.90.0060.02012.41
5.4.80.0020.02512.41
5.4.70.0030.02112.40
5.4.60.0030.02112.41
5.4.50.0080.01912.41
5.4.40.0060.02012.40
5.4.30.0010.02612.39
5.4.20.0060.01912.39
5.4.10.0040.02112.39
5.4.00.0020.02412.14
5.3.290.0050.04012.80
5.3.280.0020.04212.71
5.3.270.0050.03812.73
5.3.260.0040.03912.71
5.3.250.0030.04012.72
5.3.240.0080.03312.72
5.3.230.0030.03912.71
5.3.220.0060.03512.68
5.3.210.0060.03812.68
5.3.200.0030.03912.68
5.3.190.0070.04512.67
5.3.180.0060.03612.67
5.3.170.0060.03712.67
5.3.160.0090.03312.67
5.3.150.0050.04512.67
5.3.140.0030.03712.66
5.3.130.0070.03712.66
5.3.120.0050.03912.65
5.3.110.0080.03912.66
5.3.100.0040.04612.12
5.3.90.0050.03812.09
5.3.80.0070.04312.08
5.3.70.0070.03612.07
5.3.60.0040.03712.07
5.3.50.0020.03912.00
5.3.40.0050.03612.01
5.3.30.0050.03511.97
5.3.20.0050.03411.74
5.3.10.0030.03511.71
5.3.00.0050.03511.70
5.2.170.0030.0319.21
5.2.160.0030.0299.21
5.2.150.0040.0319.21
5.2.140.0070.0289.21
5.2.130.0020.0309.16
5.2.120.0080.0239.16
5.2.110.0080.0249.17
5.2.100.0050.0279.16
5.2.90.0030.0309.16
5.2.80.0010.0379.16
5.2.70.0050.0299.16
5.2.60.0060.0339.11
5.2.50.0070.0369.08
5.2.40.0050.0309.06
5.2.30.0080.0279.04
5.2.20.0040.0309.03
5.2.10.0040.0348.95
5.2.00.0040.0328.80
5.1.60.0030.0238.10
5.1.50.0040.0308.09
5.1.40.0070.0238.07
5.1.30.0020.0278.42
5.1.20.0060.0268.45
5.1.10.0050.0278.17
5.1.00.0040.0278.17
5.0.50.0020.0216.65
5.0.40.0020.0206.51
5.0.30.0030.0336.32
5.0.20.0020.0206.30
5.0.10.0050.0186.29
5.0.00.0030.0306.28
4.4.90.0020.0164.78
4.4.80.0040.0144.75
4.4.70.0020.0164.75
4.4.60.0020.0154.75
4.4.50.0020.0154.77
4.4.40.0040.0234.71
4.4.30.0030.0174.76
4.4.20.0020.0164.84
4.4.10.0000.0204.85
4.4.00.0020.0284.76
4.3.110.0030.0224.67
4.3.100.0060.0184.66
4.3.90.0060.0164.63
4.3.80.0050.0234.58
4.3.70.0030.0184.63
4.3.60.0050.0134.63
4.3.50.0030.0154.63
4.3.40.0040.0284.54
4.3.30.0030.0143.34
4.3.20.0030.0153.32
4.3.10.0020.0153.27
4.3.00.0070.01015.60

preferences:
45.32 ms | 401 KiB | 5 Q