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 ...?" . PHP_EOL; var_dump(to_int("10.")); 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.40.0070.00718.61
8.3.30.0140.00319.09
8.3.20.0000.00820.13
8.3.10.0040.00420.51
8.3.00.0060.00319.23
8.2.170.0090.00922.96
8.2.160.0070.01020.47
8.2.150.0040.00424.18
8.2.140.0050.00324.66
8.2.130.0040.00426.16
8.2.120.0000.00822.24
8.2.110.0060.00322.19
8.2.100.0080.00317.84
8.2.90.0050.00319.23
8.2.80.0080.00017.97
8.2.70.0030.00517.75
8.2.60.0060.00617.80
8.2.50.0050.00318.07
8.2.40.0040.00418.17
8.2.30.0040.00419.31
8.2.20.0060.00317.81
8.2.10.0040.00418.00
8.2.00.0040.00417.81
8.1.270.0000.01024.01
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0070.00323.79
8.1.230.0080.00419.22
8.1.220.0080.00017.74
8.1.210.0030.00518.77
8.1.200.0000.00917.35
8.1.190.0000.00817.35
8.1.180.0080.00018.10
8.1.170.0030.00618.48
8.1.160.0040.00422.13
8.1.150.0070.00018.83
8.1.140.0040.00417.58
8.1.130.0000.00717.89
8.1.120.0000.00717.50
8.1.110.0070.00017.43
8.1.100.0030.00517.60
8.1.90.0040.00417.55
8.1.80.0000.00717.58
8.1.70.0000.00717.43
8.1.60.0030.00617.70
8.1.50.0050.00217.59
8.1.40.0000.00817.47
8.1.30.0040.00417.74
8.1.20.0000.00817.73
8.1.10.0080.00017.58
8.1.00.0050.00317.41
8.0.300.0000.00718.77
8.0.290.0000.00716.88
8.0.280.0050.00318.51
8.0.270.0030.00717.30
8.0.260.0030.00317.35
8.0.250.0030.00317.16
8.0.240.0030.00317.14
8.0.230.0030.00317.05
8.0.220.0030.00316.96
8.0.210.0000.00717.09
8.0.200.0030.00517.15
8.0.190.0040.00417.21
8.0.180.0040.00417.06
8.0.170.0040.00417.07
8.0.160.0040.00417.03
8.0.150.0090.00017.06
8.0.140.0040.00417.08
8.0.130.0000.00613.47
8.0.120.0000.00817.11
8.0.110.0000.00716.92
8.0.100.0070.00017.04
8.0.90.0040.00417.11
8.0.80.0070.00716.96
8.0.70.0070.00017.05
8.0.60.0040.00416.96
8.0.50.0070.00017.09
8.0.30.0120.00717.16
8.0.20.0080.01217.40
8.0.10.0030.00517.00
8.0.00.0100.01017.07
7.4.330.0050.00015.15
7.4.320.0000.00616.54
7.4.300.0000.00616.68
7.4.290.0000.00816.59
7.4.280.0050.00216.68
7.4.270.0070.00016.48
7.4.260.0070.00016.58
7.4.250.0040.00416.59
7.4.240.0040.00316.67
7.4.230.0040.00416.66
7.4.220.0100.01316.61
7.4.210.0060.01316.69
7.4.200.0080.00016.75
7.4.160.0080.00816.65
7.4.150.0160.01317.40
7.4.140.0090.00817.86
7.4.130.0120.00816.67
7.4.120.0140.00616.54
7.4.110.0070.01116.72
7.4.100.0090.00916.54
7.4.90.0130.00716.56
7.4.80.0140.00419.03
7.4.70.0090.00916.57
7.4.60.0120.00616.46
7.4.50.0060.00316.65
7.4.40.0080.00816.60
7.4.30.0110.00516.46
7.4.00.0060.00915.03
7.3.330.0060.00013.42
7.3.320.0030.00313.58
7.3.310.0000.00716.37
7.3.300.0000.00716.52
7.3.290.0110.00416.55
7.3.280.0110.00916.56
7.3.270.0120.01717.40
7.3.260.0090.00916.56
7.3.250.0100.01116.49
7.3.240.0090.00916.73
7.3.230.0100.01316.57
7.3.210.0100.01016.61
7.3.200.0040.01719.39
7.3.190.0060.01516.77
7.3.180.0100.01316.37
7.3.170.0090.00916.54
7.3.160.0040.01216.70
7.2.330.0110.00816.88
7.2.320.0120.00616.60
7.2.310.0060.01216.88
7.2.300.0000.01816.88
7.2.290.0150.00316.86
7.2.60.0100.00716.95
7.2.00.0000.01219.57
7.1.200.0030.00615.49
7.1.100.0030.01018.08
7.1.70.0080.00417.21
7.1.60.0040.01819.32
7.1.50.0140.01016.66
7.1.00.0070.07022.57
7.0.200.1150.00715.06
7.0.140.0030.07322.08
7.0.60.0100.07020.02
7.0.50.0030.04317.94
7.0.40.0170.07720.16
7.0.30.0230.06320.09
7.0.20.0330.07720.23
7.0.10.0130.06020.12
7.0.00.0070.05320.20
5.6.280.0070.07021.07
5.6.210.0100.08020.54
5.6.200.0170.04018.19
5.6.190.0100.06720.34
5.6.180.0370.07720.50
5.6.170.0200.04720.46
5.6.160.0100.08320.52
5.6.150.0030.08018.14
5.6.140.0070.08318.21
5.6.130.0030.04018.24
5.6.120.0100.05020.94
5.6.110.0100.08320.98
5.6.100.0100.07721.01
5.6.90.0070.09020.90
5.6.80.0070.08020.43
5.5.350.0170.04320.55
5.5.340.0070.06018.09
5.5.330.0100.08020.38
5.5.320.0270.05720.32
5.5.310.0330.06720.34
5.5.300.0130.05018.05
5.5.290.0030.07017.98
5.5.280.0130.07021.00
5.5.270.0100.08320.89
5.5.260.0100.06720.87
5.5.250.0000.04320.60
5.5.240.0230.07020.09
5.4.450.0100.04019.48
5.4.440.0730.06319.39
5.4.430.0900.05319.55
5.4.420.0630.00019.54
5.4.410.0630.00019.45
5.4.400.0670.00019.26
5.4.390.0630.00019.20
5.4.380.0430.05718.49
5.4.370.0230.05718.80
5.4.360.0300.05718.75
5.4.350.0470.05018.75
5.4.340.0230.07318.60
5.4.320.0060.04312.53
5.4.310.0040.04012.52
5.4.300.0050.04112.53
5.4.290.0070.03812.52
5.4.280.0130.02912.42
5.4.270.0040.04112.42
5.4.260.0090.03812.42
5.4.250.0090.04312.43
5.4.240.0110.03412.42
5.4.230.0080.04612.42
5.4.220.0090.03612.41
5.4.210.0070.04412.41
5.4.200.0080.04012.41
5.4.190.0080.04012.41
5.4.180.0120.03812.41
5.4.170.0110.03512.41
5.4.160.0080.04512.41
5.4.150.0110.03912.41
5.4.140.0050.04112.09
5.4.130.0120.03012.07
5.4.120.0060.04112.04
5.4.110.0060.03512.03
5.4.100.0110.04012.03
5.4.90.0120.03112.04
5.4.80.0100.03812.04
5.4.70.0060.03812.03
5.4.60.0130.03312.02
5.4.50.0080.03212.03
5.4.40.0080.03212.02
5.4.30.0080.03712.01
5.4.20.0050.03712.02
5.4.10.0060.03612.01
5.4.00.0090.03711.50
5.3.290.0100.04312.80
5.3.280.0050.04312.71
5.3.270.0100.04512.73
5.3.260.0140.04312.72
5.3.250.0080.04312.72
5.3.240.0060.04212.72
5.3.230.0090.04312.71
5.3.220.0080.03912.68
5.3.210.0040.04312.68
5.3.200.0080.04312.68
5.3.190.0070.03912.68
5.3.180.0060.04612.67
5.3.170.0070.04312.67
5.3.160.0060.03812.67
5.3.150.0060.03812.67
5.3.140.0070.04112.66
5.3.130.0110.03912.66
5.3.120.0050.03812.66
5.3.110.0110.04312.66
5.3.100.0050.03812.11
5.3.90.0100.04312.08
5.3.80.0080.04612.08
5.3.70.0060.04612.07
5.3.60.0080.03512.06
5.3.50.0030.04012.01
5.3.40.0040.03712.01
5.3.30.0080.03811.97
5.3.20.0040.03911.74
5.3.10.0070.03911.71
5.3.00.0090.04211.70
5.2.170.0040.0319.21
5.2.160.0050.0329.21
5.2.150.0010.0389.21
5.2.140.0060.0369.20
5.2.130.0060.0369.16
5.2.120.0060.0379.17
5.2.110.0030.0359.17
5.2.100.0090.0319.16
5.2.90.0030.0319.16
5.2.80.0070.0289.16
5.2.70.0090.0329.16
5.2.60.0030.0319.12
5.2.50.0040.0429.09
5.2.40.0050.0319.06
5.2.30.0060.0309.03
5.2.20.0060.0409.02
5.2.10.0060.0308.95
5.2.00.0020.0348.80
5.1.60.0060.0298.09
5.1.50.0020.0288.09
5.1.40.0040.0258.07
5.1.30.0070.0268.41
5.1.20.0050.0298.44
5.1.10.0050.0328.17
5.1.00.0060.0238.16
5.0.50.0050.0226.64
5.0.40.0050.0216.50
5.0.30.0060.0306.32
5.0.20.0060.0276.31
5.0.10.0060.0226.29
5.0.00.0030.0426.28
4.4.90.0030.0154.78
4.4.80.0030.0154.75
4.4.70.0030.0174.76
4.4.60.0030.0184.75
4.4.50.0030.0244.77
4.4.40.0040.0294.71
4.4.30.0050.0194.76
4.4.20.0070.0164.85
4.4.10.0020.0254.85
4.4.00.0040.0284.76
4.3.110.0060.0164.67
4.3.100.0030.0264.66
4.3.90.0050.0234.63
4.3.80.0030.0264.58
4.3.70.0060.0144.63
4.3.60.0040.0194.63
4.3.50.0040.0154.63
4.3.40.0020.0264.53
4.3.30.0030.0173.34
4.3.20.0030.0183.32
4.3.10.0010.0163.27
4.3.00.0070.01315.60

preferences:
41.84 ms | 400 KiB | 5 Q