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)) { if(isset($variables[1]) && self:isBoolean($variables[1])) $replace = $variables[1]; else $replace = false; 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)
5.4.320.0040.03812.52
5.4.310.0060.03912.52
5.4.300.0070.03512.52
5.4.290.0030.04012.51
5.4.280.0030.03812.41
5.4.270.0040.03912.41
5.4.260.0040.04012.41
5.4.250.0060.03812.41
5.4.240.0050.03812.41
5.4.230.0060.03812.40
5.4.220.0030.03912.40
5.4.210.0060.03612.41
5.4.200.0060.03712.41
5.4.190.0060.03712.40
5.4.180.0040.03812.40
5.4.170.0050.03712.41
5.4.160.0050.03612.41
5.4.150.0060.03612.40
5.4.140.0040.03912.09
5.4.130.0070.04712.07
5.4.120.0070.03412.04
5.4.110.0060.03512.04
5.4.100.0070.03412.04
5.4.90.0050.03712.03
5.4.80.0050.03912.03
5.4.70.0050.03512.03
5.4.60.0060.03512.04
5.4.50.0050.03612.03
5.4.40.0080.03212.02
5.4.30.0070.03712.02
5.4.20.0040.04112.01
5.4.10.0080.03812.02
5.4.00.0040.03711.51
5.3.290.0060.03912.80
5.3.280.0060.03812.71
5.3.270.0080.03612.72
5.3.260.0050.04012.72
5.3.250.0090.03412.72
5.3.240.0050.03812.71
5.3.230.0090.04112.70
5.3.220.0050.03812.68
5.3.210.0090.03612.68
5.3.200.0070.04612.68
5.3.190.0060.04112.68
5.3.180.0030.03912.67
5.3.170.0050.03712.66
5.3.160.0070.04212.67
5.3.150.0090.03512.67
5.3.140.0060.03512.66
5.3.130.0070.03712.66
5.3.120.0080.03512.66
5.3.110.0070.03712.65
5.3.100.0030.03912.13
5.3.90.0050.03712.11
5.3.80.0060.03712.10
5.3.70.0040.03912.09
5.3.60.0050.03812.09
5.3.50.0070.03612.03
5.3.40.0060.03612.03
5.3.30.0040.03611.99
5.3.20.0060.03511.77
5.3.10.0080.03511.74
5.3.00.0030.03811.74
5.2.170.0070.0289.23
5.2.160.0040.0299.23
5.2.150.0090.0269.23
5.2.140.0050.0309.23
5.2.130.0060.0289.19
5.2.120.0030.0309.19
5.2.110.0040.0299.20
5.2.100.0020.0309.20
5.2.90.0040.0299.20
5.2.80.0060.0289.20
5.2.70.0040.0309.19
5.2.60.0060.0289.14
5.2.50.0030.0319.11
5.2.40.0050.0289.09
5.2.30.0060.0289.07
5.2.20.0030.0309.06
5.2.10.0030.0298.96
5.2.00.0040.0318.82
5.1.60.0090.0198.10
5.1.50.0070.0218.10
5.1.40.0030.0258.08
5.1.30.0040.0268.44
5.1.20.0050.0258.45
5.1.10.0050.0248.17
5.1.00.0060.0238.18
5.0.50.0030.0206.66
5.0.40.0020.0206.51
5.0.30.0030.0316.33
5.0.20.0020.0206.29
5.0.10.0060.0176.27
5.0.00.0050.0296.26
4.4.90.0020.0174.78
4.4.80.0040.0144.75
4.4.70.0020.0164.76
4.4.60.0020.0164.76
4.4.50.0000.0184.77
4.4.40.0010.0274.71
4.4.30.0010.0174.76
4.4.20.0040.0144.84
4.4.10.0040.0144.85
4.4.00.0030.0244.76
4.3.110.0030.0154.67
4.3.100.0020.0154.66
4.3.90.0030.0144.64
4.3.80.0040.0234.59
4.3.70.0050.0124.63
4.3.60.0020.0154.63
4.3.50.0000.0184.63
4.3.40.0060.0204.54
4.3.30.0030.0203.31
4.3.20.0040.0193.29
4.3.10.0020.0213.24
4.3.00.0100.0206.73

preferences:
138.44 ms | 1394 KiB | 7 Q