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 { public static function check($value, array $options = array()) { if (false === is_string($value)) { return false; } 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', 'size'] as $name) { $sign = ['min' => '<', 'max' => '>', 'size' => '!=='] if (isset($options[$name])) { $optionLength = IntFilter::check($options[$name], ['min' => 1]); if ($length $sign[$name] $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) { $length = mb_strlen($needle, '8bit'); $haystack = mb_substr($haystack, -$length); return $haystack[0] === $needle[0] ? strncmp($haystack, $needle, $length) === 0 : false; } } var_dump(StringFilter::check(' r a ', ['min' => 1])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'a'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r', 'endsWith' => 'a'])); 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']));

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)
7.1.60.1300.02035.07
7.1.50.0100.01334.72
7.1.40.1070.01332.44
7.1.30.1000.01332.48
7.1.20.1130.00732.63
7.1.10.0770.00314.64
7.1.00.0530.00714.66
7.0.200.0000.01016.49
7.0.190.0030.01016.46
7.0.180.0900.01014.29
7.0.170.0870.00714.29
7.0.160.0100.01014.20
7.0.150.0930.01314.14
7.0.140.0100.00714.44
7.0.130.0100.00314.66
7.0.120.0730.01014.46
7.0.110.0700.00714.29
7.0.100.0700.00714.26
7.0.90.0770.01314.36
7.0.80.0330.01314.26
7.0.70.0870.00714.36
7.0.60.0930.00714.16
7.0.50.0930.01314.30
7.0.40.1030.01014.54
7.0.30.1200.00714.45
7.0.20.0130.01314.39
7.0.10.1130.01014.52
7.0.00.0730.01014.35

preferences:
137.01 ms | 1394 KiB | 7 Q