3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Filter { abstract public static function check($value, array $options = array()); } final class IntFilter { public static function check($value, array $options = array()) { return $value; } } final class StringFilter { const OPTIONS = ['trim', 'empty', 'min', 'max', 'startsWith', 'endsWith']; public static function check($value, array $options = array()) { if (false === is_string($value)) { return false; } foreach(array_keys($options) as $name){ if (false === in_array($name, self::OPTIONS)) { throw new InvalidArgumentException($name . ' inconnu !'); } } if (false !== ($options['trim'] ?? true)) { $value = trim($value); } $length = mb_strlen($value, '8bit'); if ((false === ($options['empty'] ?? true)) && (0 === $length)) { return false; } foreach (['min', 'max'] as $name) { if (isset($options[$name])) { $optionLength = IntFilter::check($options[$name], ['min' => 1]); if (('min' === $name && $length < $optionLength) || ('max' === $name && $length > $optionLength)) { return false; } } } foreach (['startsWith', 'endsWith'] as $name) { if (isset($options[$name])) { $needle = StringFilter::check($options[$name]); if (false === self::$name($value, $needle)) { return false; } } } return $value; } private static function startsWith($haystack, $needle) { return $haystack[0] === $needle[0] ? strncmp($haystack, $needle, mb_strlen($needle, '8bit')) === 0 : false; } private static function endsWith($haystack, $needle) { return self::startsWith(mb_substr($haystack, -mb_strlen($needle, '8bit')), $needle); } } var_dump(StringFilter::check(' r a ', ['min' => 4])); var_dump(StringFilter::check(' r a ', ['max' => 4])); var_dump(StringFilter::check(' r a ', ['min' => 4, 'max' => 4])); var_dump(StringFilter::check(' r a ', ['min' => 5])); var_dump(StringFilter::check(' r a ', ['max' => 3])); var_dump(StringFilter::check(' r a ', ['min' => 5, 'max' => 4])); var_dump(StringFilter::check(' r a ', ['min' => 4, 'max' => 3])); var_dump(StringFilter::check(' r a ', ['min' => 5, 'max' => 3])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'a'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r', 'endsWith' => 'a'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r at'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'r at'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r at', 'endsWith' => 'r at'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r', 'endsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'z', 'endsWith' => 'a'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'z', 'endsWith' => 'y'])); var_dump(StringFilter::check(' r a ', ['test' => 4]));

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.70.0100.00618.57
8.3.60.0180.00418.56
8.3.50.0030.01816.64
8.3.40.0160.00318.76
8.3.30.0120.00318.86
8.3.20.0040.00419.02
8.3.10.0000.00821.90
8.3.00.0080.00020.95
8.2.180.0120.00918.50
8.2.170.0060.00922.96
8.2.160.0110.00322.12
8.2.150.0040.00424.18
8.2.140.0060.00924.66
8.2.130.0000.00820.60
8.2.120.0080.00026.35
8.2.110.0090.00020.60
8.2.100.0110.00018.16
8.2.90.0000.00819.21
8.2.80.0000.00818.05
8.2.70.0100.00217.63
8.2.60.0050.00318.16
8.2.50.0050.00318.10
8.2.40.0060.00322.34
8.2.30.0030.00619.40
8.2.20.0080.00018.28
8.2.10.0070.00018.17
8.2.00.0000.00818.09
8.1.280.0110.00725.92
8.1.270.0120.00323.99
8.1.260.0080.00026.35
8.1.250.0050.00328.09
8.1.240.0090.00022.68
8.1.230.0040.00719.34
8.1.220.0040.00417.89
8.1.210.0060.00318.77
8.1.200.0060.00317.48
8.1.190.0040.00417.47
8.1.180.0030.00618.10
8.1.170.0000.00818.75
8.1.160.0040.00419.17
8.1.150.0040.00418.86
8.1.140.0080.00019.68
8.1.130.0000.00718.96
8.1.120.0050.00317.56
8.1.110.0000.00817.52
8.1.100.0040.00417.46
8.1.90.0000.00817.46
8.1.80.0040.00417.45
8.1.70.0040.00417.46
8.1.60.0000.00817.73
8.1.50.0040.00417.57
8.1.40.0040.00417.72
8.1.30.0040.00417.68
8.1.20.0050.00217.77
8.1.10.0030.00517.64
8.1.00.0080.00017.60
8.0.300.0000.00818.77
8.0.290.0040.00416.88
8.0.280.0040.00418.59
8.0.270.0030.00517.43
8.0.260.0030.00616.91
8.0.250.0030.00317.07
8.0.240.0000.00717.16
8.0.230.0070.00017.19
8.0.220.0040.00417.13
8.0.210.0030.00317.11
8.0.200.0030.00317.17
8.0.190.0000.00817.24
8.0.180.0000.00817.20
8.0.170.0070.00017.04
8.0.160.0000.00717.05
8.0.150.0030.00617.01
8.0.140.0070.00017.11
8.0.130.0030.00313.52
8.0.120.0030.00516.99
8.0.110.0000.00717.08
8.0.100.0000.00816.97
8.0.90.0000.00717.09
8.0.80.0110.01117.07
8.0.70.0020.00517.09
8.0.60.0030.00517.17
8.0.50.0040.00417.10
8.0.30.0110.00717.20
8.0.20.0120.01117.42
8.0.10.0040.00416.99
8.0.00.0120.00616.88
7.4.330.0050.00015.55
7.4.320.0060.00016.70
7.4.300.0060.00016.72
7.4.290.0070.00016.58
7.4.280.0060.00316.68
7.4.270.0080.00016.61
7.4.260.0030.00313.32
7.4.250.0040.00416.44
7.4.240.0070.00016.55
7.4.230.0040.00416.59
7.4.220.0170.00916.67
7.4.210.0000.01416.58
7.4.200.0040.00416.68
7.4.160.0100.00916.59
7.4.150.0070.01117.40
7.4.140.0080.01217.86
7.4.130.0160.01116.66
7.4.120.0120.00916.74
7.4.110.0150.00416.63
7.4.100.0150.00416.57
7.4.90.0120.00616.74
7.4.80.0110.01119.39
7.4.70.0110.00716.48
7.4.60.0090.00616.66
7.4.50.0060.00616.77
7.4.40.0080.00816.69
7.4.30.0130.00316.77
7.4.00.0100.00715.13
7.3.330.0000.00513.54
7.3.320.0030.00313.50
7.3.310.0030.00516.63
7.3.300.0000.00716.47
7.3.290.0100.00916.51
7.3.280.0060.01016.52
7.3.270.0070.01017.40
7.3.260.0070.01416.63
7.3.250.0130.00916.62
7.3.240.0150.00716.58
7.3.230.0300.03216.66
7.3.210.0100.00716.64
7.3.200.0030.01316.63
7.3.190.0120.00416.52
7.3.180.0030.01316.75
7.3.170.0000.01616.51
7.3.160.0090.00916.67
7.3.10.0060.00916.46
7.3.00.0070.01016.58
7.2.330.0090.00916.74
7.2.320.0040.01516.83
7.2.310.0130.00317.01
7.2.300.0090.00916.75
7.2.290.0100.00816.94
7.2.130.0130.00316.87
7.2.120.0060.00916.93
7.2.110.0070.01316.99
7.2.100.0030.00716.89
7.2.90.0030.00916.57
7.2.80.0090.00916.93
7.2.70.0000.01616.91
7.2.60.0050.01016.85
7.2.50.0060.00616.64
7.2.40.0030.01216.88
7.2.30.0100.00616.89
7.2.20.0000.01416.95
7.2.10.0070.00316.82
7.2.00.0070.00617.93
7.1.250.0100.00315.82
7.1.240.0040.00715.66
7.1.230.0000.01415.70
7.1.220.0130.00315.72
7.1.210.0090.00615.59
7.1.200.0080.00515.56
7.1.190.0110.00715.47
7.1.180.0000.01315.92
7.1.170.0060.00915.55
7.1.160.0070.00715.68
7.1.150.0070.01015.78
7.1.140.0030.00915.68
7.1.130.0080.00715.84
7.1.120.0100.00315.50
7.1.110.0090.00315.79
7.1.100.0020.00917.05
7.1.90.0090.00615.82
7.1.80.0000.01115.62
7.1.70.0040.00616.40
7.1.60.0590.01124.41
7.1.50.0070.01424.49
7.1.40.0110.01124.24
7.1.30.0200.00724.24
7.1.20.0100.01424.28
7.1.10.0500.00815.37
7.1.00.0030.01315.40
7.0.330.0070.00715.22
7.0.320.0000.01015.57
7.0.310.0040.00414.97
7.0.300.0000.00815.31
7.0.290.0070.00715.05
7.0.280.0110.00415.26
7.0.270.0100.00715.38
7.0.260.0060.00615.39
7.0.250.0040.01115.07
7.0.240.0050.00815.45
7.0.230.0030.01015.27
7.0.220.0060.00715.43
7.0.210.0040.00715.30
7.0.200.0050.00715.20
7.0.190.0080.00615.06
7.0.180.0100.00614.95
7.0.170.0080.00614.83
7.0.160.0030.01014.97
7.0.150.0600.00515.02
7.0.140.0130.01314.94
7.0.130.0500.01215.09
7.0.120.0600.00815.21
7.0.110.0360.00715.17
7.0.100.0040.01014.91
7.0.90.0550.01015.06
7.0.80.0070.00715.04
7.0.70.0030.01115.04
7.0.60.0530.00814.90
7.0.50.0070.00815.07
7.0.40.0820.01014.04
7.0.30.0650.01313.99
7.0.20.0520.01014.16
7.0.10.0520.00514.19
7.0.00.0480.01114.14
5.6.380.0080.00613.87

preferences:
48.55 ms | 401 KiB | 5 Q