3v4l.org

run code in 300+ PHP versions simultaneously
<?php class __Utils { public static function setHeader(string $header, $variable, $replace = false, $httpresponse = null) { if($httpresponse !== null) { header("$header: $variable", $replace, $httpresponse); } else { header("$header: $variable", $replace); } } public static function setHeaders(array $headers) { foreach($headers as $header => $variables){ if(is_array($variables)) { $replace = (isset($variables[1]) && self::isBoolean($variables[1])) ? $variables[1] : true; if( isset($variables[2]) && self::isInteger($variables[2]) ) { self::setHeader($header, $variables[0], $replace, $variables[2]); } else { self::setHeader($header, $variables[0], $replace); } } else { self::setHeader($header, $variables); } } } public static function spaces($count, $htmlentities = true) { $spaces = str_repeat('&nbsp;', (int) $count); return ($htmlentities) ? $spaces : str_replace('&nbsp;', '', $spaces); } public static function tabs($count, $tabsize = 6) { return self::spaces(((int) $tabsize * (int) $count)); } public static function lineBreaks($count, $heightpx = 16) { return str_repeat('<br style="height: ' . $heightpx . 'px;" />', (int) $count); } public static function isArray($variable) { return (is_array($variable)); } public static function isBoolean($variable) { return (is_bool($variable)); } public static function isString($variable) { return (is_string($variable)); } public static function isNull($variable) { return (is_null($variable)); } public static function isCallable($variable) { return (is_callable($variable)); } public static function isObject($variable) { return (is_object($variable)); } public static function isDouble($variable) { return (is_double($variable)); } public static function isFloat($variable) { return (is_float($variable)); } public static function isInteger($variable) { return (is_int($variable)); } public static function isLong($variable) { return (is_long($variable)); } public static function isResource($variable) { return (is_resource($variable)); } public static function getVarType($variable) { $typecases = array( "array" => self::isArray($variable), "boolean" => self::isBoolean($variable), "string" => self::isString($variable), "null" => self::isNull($variable), "callable" => self::isCallable($variable), "object" => self::isObject($variable), "double" => self::isDouble($variable), "integer" => self::isInteger($variable), "float" => self::isFloat($variable), "long" => self::isLong($variable), "resource" => self::isResource($variable) ); foreach($typecases as $type => $bool){ if($bool) return $type; } } public static function nocacheHaders() { if (!headers_sent()) { self::setHeaders(array( "Expires" => "Wed, 11 Jan 1984 05:00:00 GMT", "Last-Modified" => gmdate( 'D, d M Y H:i:s' ) . " GMT", "Cache-Control" => "no-cache, must-revalidate, max-age=0", "Pragma" => "no-cache", )); } } public static function stripSpace(string $string) { return str_replace(' ', '', preg_replace('/\s+/', '', $string)); } public static function sanitize(string $string) { $sanitized = $string; $sanitized = strip_tags($sanitized); $sanitized = htmlspecialchars($sanitized, ENT_QUOTES, 'utf-8'); $sanitized = htmlentities($sanitized, ENT_HTML5, 'utf-8'); return $sanitized; } public static function dumpVar($variable, $doreturn = false, $dopre = true, $styles = true, $tabamt = 1) { $return = ''; $wrapper = ($dopre === null) ? 'none' : ((self::isBoolean($dopre)) ? (($dopre) ? 'pre' : 'div' ) : $dopre ); $class = ($styles) ? 'styled' : 'nostyles'; $settings = array( 'wrappers' => array( 'none' => '%s', 'pre' => '<pre class="vardump ' . $class . '">%s</pre>', 'div' => '<div class="vardump ' . $class . '">%s</div>' ), 'sprintf' => array( 'vartype' => '<span class="vartype">%s</span>', 'specialvartype' => '<span class="specialvartype">%s</span>', 'parentheses' => '<span class="parentheses">(</span><span class="parcont">%s</span><span class="parentheses">)</span>', 'string' => '<span class="misc">"%s"</span>', 'raw' => '<span class="misc">%s</span>', 'arrkey' => '<span class="misc">' . self::tabs($tabamt) . '[%s]</span>', 'arrsep' => '<span class="arrsep">=></span>' ), ); switch(self::getVarType($variable)) { case "boolean": $return .= sprintf($settings['sprintf']['vartype'], 'bool') . sprintf($settings['sprintf']['parentheses'], (string) $variable ? 'true' : 'false'); break; case "integer": $return .= sprintf($settings['sprintf']['vartype'], 'int') . sprintf($settings['sprintf']['parentheses'], (string) $variable); break; case "double": $return .= sprintf($settings['sprintf']['vartype'], 'double') . sprintf($settings['sprintf']['parentheses'], (string) $variable); break; case "float": $return .= sprintf($settings['sprintf']['vartype'], 'float') . sprintf($settings['sprintf']['parentheses'], (string) $variable); break; case "string": $return .= sprintf($settings['sprintf']['vartype'], 'string') . sprintf($settings['sprintf']['parentheses'], (string) strlen($variable)) . " " . sprintf($settings['sprintf']['string'], $variable); break; case "null": $return .= sprintf($settings['sprintf']['specialvartype'], 'NULL'); break; case "array": $brackets = array('{'); foreach($variable as $key => $value) { $brackets[] = sprintf($settings['sprintf']['arrkey'], $key) . " " . $settings['sprintf']['arrsep'] . " " . self::dumpVar($value, true, null, $styles, $tabamt + 1) . ','; } $brackets[] = ($tabamt < 2) ? '}' : self::tabs(($tabamt - 1)) . '}'; $brackets = ($dopre) ? implode("\n", $brackets) : implode('<br />', $brackets); $return .= sprintf($settings['sprintf']['vartype'], 'array') . sprintf($settings['sprintf']['parentheses'], (string) count($variable)) . " " . $brackets; break; case "object": $reflclass = new ReflectionClass($variable); $constants = $reflclass->getConstants(); $variables = $reflclass->getProperties(); $methods = $reflclass->getMethods(); $brackets = array('{'); foreach($constants as $name => $value) { $brackets[] = self::tabs($tabamt) . sprintf($settings['sprintf']['raw'], 'CONST ' . $name) . ' ' . self::dumpVar($value, true, null, $styles); } foreach($variables as $var) { } $return .= sprintf($settings['sprintf']['vartype'], 'object') . sprintf($settings['sprintf']['parentheses'], get_class($variable)); break; default: $return .= sprintf($settings['sprintf']['specialvartype'], 'UNKNOWN TYPE') . ' ' . sprintf($settings['sprintf']['raw'], var_export($variable, true)); break; } if($dopre) $return = str_ireplace('&nbsp;', ' ', $return); $returnwrapper = (array_key_exists($wrapper, $settings['wrappers'])) ? $settings['wrappers'][$wrapper] : $settings['wrappers']['none']; $return = sprintf($returnwrapper, $return); if($doreturn) return $return; echo $return; } public static function dumpVars($variables, $doreturn = false, $dopre = true, $styles = true, $tabamt = 1) { $return = array(); foreach($variables as $var) { $return[] = self::dumpVar($var, true, $dopre, $styles, $tabamt); } $return = implode(self::lineBreaks(1), $return); if($doreturn) return $return; echo $return; } public static function outputData(string $filename, string $content = null, $mimetype = "auto") { $mimetype = (is_file($filename)) ? finfo_file(finfo_open(FILEINFO_MIME, pathinfo($content, PATHINFO_DIRNAME), $content)) : ($mimetype == "auto") ? "application/force-download" : $mimetype; if(!headers_sent()) { $output = is_null($content) ? file_get_contents($file) : $content; self:setHeaders(array( "Pragma" => "public", "Expires" => "0", "Cache-Control" => "must-revalidate, post-check=0, pre-check=0", "Cache-Control" => array("private", FALSE), "Content-Disposition" => "attatchment; filename=" . basename(str_replace('"', '', $filename)) . '";', "Content-Type" => $mimetype, "Content-Transer-Encoding" => "binary", "Content-Length" => strlen($output), )); ob_clean(); flush(); echo $output; exit(); } } public static function forceDownload($file, $content = null) { self::outputData($file, $content, "application/force-download"); } public static function uniqueId($prefix = null, $format = '{35}') // Ex: {8}-{4}-{4}-{4}-{12} will return something like XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX { $uniqueid = $prefix; $alpha = range('a', 'z', 1); $alphacase = range('A', 'Z', 1); $num = range('1', '999', 1); $rand1 = array_rand($alpha, 5); $rand2 = array_rand($alphacase, 5); $rand3 = array_rand($num, 5); $rand = array_merge($rand1, $rand2, $rand3); $datetime = new DateTime('now', new DateTimeZone('America/New_York')); $timestamp = $datetime->format('c'); $uniqueid .= implode('', $rand) . "_" . $timestamp . "_" . uniqid(); $origuniqueid = $uniqueid; foreach(hash_algos() as $algo) { $uniqueid = hash($algo, $uniqueid . "_" . $timestamp . "_" . uniqid()); } $uniqueid = md5($uniqueid); $chararrays = array(); $finaluniqueid = ''; $amount = 10; for($time = $amount; $time > 0; $time--) { $chararrays[$time] = str_split($uniqueid); shuffle($chararrays[$time]); $finaluniqueid .= implode('', array_slice($chararrays[$time], rand(0, (count($chararrays[$time]) - 1)), rand(0, (count($chararrays[$time]) - 1)))); } $finaluniqueid = str_shuffle($finaluniqueid) . str_shuffle($finaluniqueid) . str_shuffle($finaluniqueid) . str_shuffle($finaluniqueid) . str_shuffle($finaluniqueid); $formateduniqueid = $format; $substart = 0; preg_match_all('([0-9]+)', $format, $ints); foreach($ints[0] as $int) { if($newi > strlen($formateduniqueid) ) $newi = strlen($formateduniqueid); $substr = substr($finaluniqueid, $substart, ((int) $int)); $formateduniqueid = preg_replace('/\{' . $int . '\}/', $substr, $formateduniqueid, 1); $substart = $substart + ((int) $int); } return $formateduniqueid; } public static function getRelativePath($from, $to) { $from = is_dir($from) ? rtrim($from, '\/') . '/' : $from; $to = is_dir($to) ? rtrim($to, '\/') . '/' : $to; $from = str_replace('\\', '/', $from); $to = str_replace('\\', '/', $to); $from = explode('/', $from); $to = explode('/', $to); $relPath = $to; foreach($from as $depth => $dir) { if($dir === $to[$depth]) { array_shift($relPath); } else { $remaining = count($from) - $depth; if($remaining > 1) { $padLength = (count($relPath) + $remaining - 1) * -1; $relPath = array_pad($relPath, $padLength, '..'); break; } else { $relPath[0] = './' . $relPath[0]; } } } return implode('/', $relPath); } public static function arrayList(array $array, $withkeys = false, $keysep = ': ') { if($withkeys) { foreach($array as $k => $v) { $array[$k] = $k . $keysep . $v; } } switch(count($array)) { case 1: return implode('', $array); break; case 2: return implode(' and ', $array); break; case 3: $arrv = array_values($array); $array[array_search($arrv[(count($arrv) - 1)], $array)] = 'and ' . $array[array_search($arrv[(count($arrv) - 1)], $array)]; $output = implode(', ', $array); if(strtoupper(Locale::getRegion(Locale::getDefault())) === 'GB') $output = str_replace(', and', ' and ', $output); return $output; } } public static function pluckArrays($key, $value, $arrays) { $return = array(); foreach($arrays as $array) { if($array[$key] === $value) { $return[] = $array; } } return $return; } public static function addQuotes($string, $quote = '"') { return $quote . $string . $quote; } }

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.0150.00618.43
8.3.50.0170.00321.81
8.3.40.0090.01218.82
8.3.30.0000.01519.09
8.3.20.0040.00420.34
8.3.10.0060.00320.40
8.3.00.0050.00319.55
8.2.180.0040.01516.63
8.2.170.0090.00622.96
8.2.160.0140.00020.50
8.2.150.0000.00824.18
8.2.140.0040.00424.66
8.2.130.0060.00326.16
8.2.120.0080.00022.06
8.2.110.0100.01321.05
8.2.100.0060.00617.71
8.2.90.0090.00019.34
8.2.80.0000.00817.97
8.2.70.0030.00617.75
8.2.60.0030.00517.93
8.2.50.0050.00318.10
8.2.40.0040.00418.25
8.2.30.0030.00519.37
8.2.20.0000.00817.76
8.2.10.0060.00318.20
8.2.00.0040.00417.84
8.1.280.0040.01125.92
8.1.270.0070.00322.31
8.1.260.0000.00926.35
8.1.250.0080.00028.09
8.1.240.0080.00322.43
8.1.230.0090.00319.17
8.1.220.0050.00317.91
8.1.210.0060.00318.77
8.1.200.0020.00917.38
8.1.190.0000.00817.35
8.1.180.0050.00318.10
8.1.170.0040.00418.75
8.1.160.0000.00822.16
8.1.150.0000.00819.02
8.1.140.0000.00817.59
8.1.130.0020.00518.07
8.1.120.0040.00417.58
8.1.110.0040.00417.44
8.1.100.0000.00817.64
8.1.90.0000.00717.44
8.1.80.0040.00417.55
8.1.70.0070.00017.51
8.1.60.0000.00817.57
8.1.50.0090.00017.55
8.1.40.0040.00417.57
8.1.30.0030.00617.59
8.1.20.0040.00417.65
8.1.10.0030.00517.65
8.1.00.0030.00617.57
8.0.300.0040.00418.77
8.0.290.0050.00316.75
8.0.280.0070.00018.53
8.0.270.0040.00416.95
8.0.260.0030.00317.34
8.0.250.0000.00817.11
8.0.240.0000.00716.96
8.0.230.0030.00617.07
8.0.220.0050.00217.07
8.0.210.0040.00417.05
8.0.200.0000.00717.16
8.0.190.0040.00417.02
8.0.180.0030.00516.96
8.0.170.0000.00816.91
8.0.160.0070.00017.05
8.0.150.0000.00816.88
8.0.140.0080.00016.93
8.0.130.0030.00313.48
8.0.120.0000.00817.02
8.0.110.0060.00317.01
8.0.100.0050.00316.89
8.0.90.0000.00817.08
8.0.80.0070.01017.02
8.0.70.0100.00017.11
8.0.60.0020.00517.05
8.0.50.0050.00317.05
8.0.30.0130.00717.00
8.0.20.0130.01117.40
8.0.10.0000.00817.05
8.0.00.0090.01416.98
7.4.330.0020.00415.00
7.4.320.0070.00016.60
7.4.300.0030.00316.66
7.4.290.0040.00416.65
7.4.280.0110.00016.64
7.4.270.0030.00316.72
7.4.260.0000.00816.70
7.4.250.0000.00816.71
7.4.240.0020.00616.69
7.4.230.0040.00416.72
7.4.220.0090.01916.65
7.4.210.0100.01016.79
7.4.200.0000.00716.81
7.4.160.0140.01016.75
7.4.150.0200.00017.40
7.4.140.0060.01217.86
7.4.130.0080.00816.65
7.4.120.0110.00716.64
7.4.110.0090.00916.63
7.4.100.0100.01016.64
7.4.90.0040.01316.60
7.4.80.0100.01016.63
7.4.70.0090.00916.76
7.4.60.0060.01216.52
7.4.50.0000.00516.38
7.4.40.0110.00616.67
7.4.30.0030.01316.60
7.4.00.0050.01315.18
7.3.330.0030.00313.40
7.3.320.0000.00913.40
7.3.310.0000.00716.42
7.3.300.0000.00716.25
7.3.290.0030.01116.38
7.3.280.0140.00316.33
7.3.270.0070.01117.40
7.3.260.0070.01816.54
7.3.250.0140.00716.44
7.3.240.0080.00816.52
7.3.230.0150.00916.63
7.3.210.0130.00716.46
7.3.200.0030.02019.39
7.3.190.0120.00616.71
7.3.180.0070.01016.47
7.3.170.0030.01316.51
7.3.160.0080.00816.64
7.3.120.0070.01414.96
7.3.110.0080.00314.59
7.3.100.0070.01414.89
7.3.90.0030.01315.09
7.3.80.0080.00414.60
7.3.70.0060.00914.75
7.3.60.0060.00614.70
7.3.50.0090.00614.79
7.3.40.0030.01014.65
7.3.30.0030.00614.80
7.3.20.0070.01016.22
7.3.10.0070.00816.50
7.3.00.0060.01116.42
7.2.330.0150.00316.44
7.2.320.0140.00716.66
7.2.310.0060.01316.53
7.2.300.0100.01016.61
7.2.290.0160.00616.55
7.2.250.0100.01015.12
7.2.240.0030.01314.75
7.2.230.0000.01215.29
7.2.220.0060.00914.91
7.2.210.0080.00514.98
7.2.200.0000.01715.19
7.2.190.0030.01214.83
7.2.180.0030.00714.73
7.2.170.0080.00815.19
7.2.160.0030.01015.05
7.2.150.0000.01316.35
7.2.140.0070.01316.85
7.2.130.0090.00516.57
7.2.120.0080.00716.58
7.2.110.0050.01216.60
7.2.100.0000.01716.55
7.2.90.0030.00716.81
7.2.80.0060.00516.69
7.2.70.0110.00416.60
7.2.60.0070.00616.67
7.2.50.0050.01016.59
7.2.40.0050.00716.57
7.2.30.0060.00716.59
7.2.20.0050.01116.71
7.2.10.0070.00616.75
7.2.00.0060.01117.40
7.1.330.0070.01015.66
7.1.320.0100.00715.55
7.1.310.0000.01315.67
7.1.300.0030.01015.76
7.1.290.0070.00715.64
7.1.280.0070.00715.54
7.1.270.0100.00315.39
7.1.260.0140.00715.37
7.1.250.0070.00715.64
7.1.240.0040.01415.64
7.1.230.0100.01015.66
7.1.220.0090.00615.63
7.1.210.0030.01215.35
7.1.200.0060.00715.59
7.1.190.0080.00615.56
7.1.180.0040.01415.47
7.1.170.0030.00915.66
7.1.160.0040.00815.68
7.1.150.0040.01115.48
7.1.140.0050.00515.72
7.1.130.0060.01215.42
7.1.120.0090.00315.66
7.1.110.0070.00715.59
7.1.100.0070.00516.58
7.1.90.0000.01315.81
7.1.80.0000.01415.68
7.1.70.0050.00516.43
7.1.60.0070.01017.53
7.1.50.0040.01016.24
7.1.40.0040.01215.33
7.1.30.0040.01215.70
7.1.20.0030.01015.47
7.1.10.0070.01115.55
7.1.00.0050.04018.86
7.0.330.0070.00715.27
7.0.320.0090.00915.42
7.0.310.0060.00915.05
7.0.300.0090.00615.20
7.0.290.0030.00815.19
7.0.280.0090.00315.37
7.0.270.0060.00915.00
7.0.260.0060.00615.19
7.0.250.0060.01015.52
7.0.240.0080.00815.27
7.0.230.0030.01015.21
7.0.220.0120.00315.31
7.0.210.0070.00715.25
7.0.200.0080.00515.88
7.0.190.0060.00615.04
7.0.180.0040.00815.41
7.0.170.0040.00715.31
7.0.160.0060.00615.46
7.0.150.0030.00715.24
7.0.140.0040.05018.68
7.0.130.0110.00314.99
7.0.120.0040.00415.25
7.0.110.0060.00615.30
7.0.100.0100.04417.78
7.0.90.0180.03417.77
7.0.80.0210.03917.72
7.0.70.0200.04217.76
7.0.60.0220.04017.75
7.0.50.0070.05018.64
7.0.40.0100.02516.57
7.0.30.0100.04516.66
7.0.20.0050.04916.65
7.0.10.0050.04416.63
7.0.00.0070.03616.62
5.6.400.0130.00314.50
5.6.390.0060.00914.55
5.6.380.0100.01014.22
5.6.370.0060.00614.52
5.6.360.0080.00314.35
5.6.350.0120.00314.35
5.6.340.0030.00614.31
5.6.330.0030.01514.26
5.6.320.0040.01114.00
5.6.310.0000.01014.00
5.6.300.0000.01814.35
5.6.290.0060.01014.15
5.6.280.0100.03717.83
5.6.270.0060.01014.42
5.6.260.0030.00614.39
5.6.250.0080.04317.63
5.6.240.0110.04017.43
5.6.230.0020.03117.47
5.6.220.0020.05017.33
5.6.210.0100.03817.64
5.6.200.0100.04017.56
5.6.190.0090.04217.71
5.6.180.0020.04517.71
5.6.170.0070.04017.57
5.6.160.0100.03517.68
5.6.150.0120.03017.51
5.6.140.0090.04217.58
5.6.130.0070.03317.59
5.6.120.0060.02317.78
5.6.110.0050.04017.79
5.6.100.0070.04317.55
5.6.90.0080.02217.71
5.6.80.0110.03717.31
5.6.70.0080.02617.32
5.6.60.0070.04217.42
5.6.50.0050.02717.38
5.6.40.0050.03817.43
5.6.30.0050.03717.49
5.6.20.0110.01917.40
5.6.10.0130.04017.23
5.6.00.0050.04417.23
5.5.380.0070.04517.49
5.5.370.0000.05117.31
5.5.360.0050.04817.43
5.5.350.0080.04017.43
5.5.340.0150.04017.66
5.5.330.0120.03717.69
5.5.320.0100.03917.49
5.5.310.0050.03117.56
5.5.300.0070.04817.61
5.5.290.0040.05317.53
5.5.280.0120.04017.46
5.5.270.0050.03117.32
5.5.260.0090.03617.33
5.5.250.0020.04417.38
5.5.240.0070.03417.06
5.5.230.0020.02817.27
5.5.220.0050.04317.01
5.5.210.0030.03017.14
5.5.200.0050.04017.22
5.5.190.0110.02317.09
5.5.180.0040.04217.27
5.5.170.0060.01013.97
5.5.160.0080.03817.31
5.5.150.0100.02517.04
5.5.140.0060.04517.00
5.5.130.0070.04417.21
5.5.120.0100.04117.12
5.5.110.0050.04817.18
5.5.100.0100.04417.04
5.5.90.0110.04017.29
5.5.80.0050.03117.22
5.5.70.0070.04217.13
5.5.60.0080.02317.08
5.5.50.0030.03717.21
5.5.40.0070.04217.10
5.5.30.0070.03717.22
5.5.20.0120.03817.02
5.5.10.0030.04217.10
5.5.00.0050.04416.89
5.4.450.0040.02916.28
5.4.440.0030.04316.34
5.4.430.0080.04016.28
5.4.420.0080.03116.33
5.4.410.0030.02616.32
5.4.400.0030.02716.23
5.4.390.0080.03216.14
5.4.380.0070.04216.06
5.4.370.0060.03616.12
5.4.360.0070.04016.14
5.4.350.0050.03416.17
5.4.340.0110.03716.16
5.4.330.0000.01413.18
5.4.320.0050.04416.21
5.4.310.0030.04516.13
5.4.300.0060.03816.16
5.4.290.0080.03816.04
5.4.280.0080.04016.21
5.4.270.0030.03616.13
5.4.260.0030.04516.16
5.4.250.0080.01816.13
5.4.240.0090.03616.14
5.4.230.0090.03616.15
5.4.220.0090.03716.06
5.4.210.0070.02216.22
5.4.200.0020.04316.06
5.4.190.0060.04016.17
5.4.180.0030.04516.06
5.4.170.0110.03816.15
5.4.160.0080.04016.12
5.4.150.0070.04016.13
5.4.140.0070.04314.85
5.4.130.0050.03814.83
5.4.120.0030.04314.81
5.4.110.0090.03414.80
5.4.100.0020.03014.81
5.4.90.0070.03514.81
5.4.80.0060.02514.82
5.4.70.0030.04514.87
5.4.60.0070.02414.88
5.4.50.0090.03714.86
5.4.40.0020.04714.86
5.4.30.0030.02814.83
5.4.20.0070.03314.88
5.4.10.0070.03714.81
5.4.00.0070.02414.59

preferences:
46.57 ms | 401 KiB | 5 Q