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.0060.01118.31
8.3.50.0100.01321.93
8.3.40.0150.00018.84
8.3.30.0040.01119.17
8.3.20.0040.00420.61
8.3.10.0080.00020.53
8.3.00.0080.00019.31
8.2.180.0110.00716.88
8.2.170.0090.00922.96
8.2.160.0110.00320.72
8.2.150.0030.00624.18
8.2.140.0050.00324.66
8.2.130.0090.00026.16
8.2.120.0040.00422.23
8.2.110.0000.01121.18
8.2.100.0040.00817.97
8.2.90.0060.00319.25
8.2.80.0000.00817.97
8.2.70.0070.00317.75
8.2.60.0030.00618.17
8.2.50.0100.00018.10
8.2.40.0000.00818.34
8.2.30.0040.00419.45
8.2.20.0040.00417.68
8.2.10.0000.00818.08
8.2.00.0040.00417.82
8.1.280.0070.00725.92
8.1.270.0040.00422.35
8.1.260.0050.00326.35
8.1.250.0060.00328.09
8.1.240.0030.00922.45
8.1.230.0000.01319.05
8.1.220.0000.00817.91
8.1.210.0100.00018.77
8.1.200.0110.00017.48
8.1.190.0000.00817.23
8.1.180.0040.00418.10
8.1.170.0060.00318.75
8.1.160.0000.00822.18
8.1.150.0080.00018.98
8.1.140.0000.00817.49
8.1.130.0000.00818.00
8.1.120.0060.00317.57
8.1.110.0000.00817.43
8.1.100.0030.00617.54
8.1.90.0000.00717.53
8.1.80.0050.00317.41
8.1.70.0050.00217.57
8.1.60.0030.00517.57
8.1.50.0030.00617.65
8.1.40.0030.00617.51
8.1.30.0000.00917.75
8.1.20.0040.00417.79
8.1.10.0040.00417.63
8.1.00.0050.00317.52
8.0.300.0060.00318.77
8.0.290.0040.00416.75
8.0.280.0040.00418.58
8.0.270.0000.00717.25
8.0.260.0030.00317.39
8.0.250.0040.00317.13
8.0.240.0080.00017.00
8.0.230.0080.00017.07
8.0.220.0040.00417.07
8.0.210.0020.00517.05
8.0.200.0040.00416.98
8.0.190.0040.00417.01
8.0.180.0040.00417.04
8.0.170.0040.00417.04
8.0.160.0080.00016.96
8.0.150.0030.00616.89
8.0.140.0030.00517.00
8.0.130.0000.00613.48
8.0.120.0030.00617.04
8.0.110.0040.00417.02
8.0.100.0050.00216.90
8.0.90.0030.00617.01
8.0.80.0040.01117.10
8.0.70.0040.00517.06
8.0.60.0080.00016.85
8.0.50.0040.00416.94
8.0.30.0040.01517.11
8.0.20.0150.00917.40
8.0.10.0040.00416.96
8.0.00.0090.00916.94
7.4.330.0000.00715.00
7.4.320.0070.00016.62
7.4.300.0040.00416.69
7.4.290.0060.00316.68
7.4.280.0060.00616.72
7.4.270.0030.00416.73
7.4.260.0040.00416.50
7.4.250.0040.00416.56
7.4.240.0040.00416.57
7.4.230.0000.00716.54
7.4.220.0140.01416.79
7.4.210.0090.00616.57
7.4.200.0040.00416.69
7.4.160.0100.00716.59
7.4.150.0070.01117.40
7.4.140.0090.01117.86
7.4.130.0070.01216.59
7.4.120.0110.00816.66
7.4.110.0090.00916.72
7.4.100.0150.00616.59
7.4.90.0120.00616.65
7.4.80.0030.01416.72
7.4.70.0130.01016.80
7.4.60.0060.01216.62
7.4.50.0030.00616.75
7.4.40.0120.00616.84
7.4.30.0100.00716.57
7.4.00.0080.00414.77
7.3.330.0030.00313.42
7.3.320.0050.00313.27
7.3.310.0040.00416.29
7.3.300.0000.00716.39
7.3.290.0030.01016.38
7.3.280.0050.01116.40
7.3.270.0030.02117.40
7.3.260.0110.00616.52
7.3.250.0090.00916.40
7.3.240.0180.00016.36
7.3.230.0120.00616.39
7.3.210.0080.01116.63
7.3.200.0160.00619.39
7.3.190.0140.00716.39
7.3.180.0070.01016.47
7.3.170.0050.01416.56
7.3.160.0120.00416.69
7.3.10.0000.01216.70
7.3.00.0000.01016.28
7.2.330.0060.01216.67
7.2.320.0110.00716.72
7.2.310.0120.00616.71
7.2.300.0000.01816.74
7.2.290.0140.00616.53
7.2.130.0040.01216.64
7.2.120.0060.00916.79
7.2.110.0070.00716.61
7.2.100.0070.00716.63
7.2.90.0030.00716.46
7.2.80.0060.00916.53
7.2.70.0030.01216.81
7.2.60.0100.00716.79
7.2.50.0030.00716.86
7.2.40.0070.00716.75
7.2.30.0060.01016.67
7.2.20.0110.00716.57
7.2.10.0080.00816.43
7.2.00.0020.01117.73
7.1.250.0090.00015.77
7.1.200.0040.00815.57
7.1.100.0080.00817.84
7.1.70.0000.00716.68
7.1.60.0070.01819.32
7.1.50.0060.01916.90
7.1.00.0100.07022.40
7.0.200.0070.00316.73
7.0.140.0000.07721.94
7.0.100.0070.09720.18
7.0.90.0130.06720.21
7.0.80.0070.08320.34
7.0.70.0030.08020.13
7.0.60.0070.06320.09
7.0.50.0470.05320.50
7.0.40.0130.07320.11
7.0.30.0030.08320.11
7.0.20.0030.08320.18
7.0.10.0100.07319.91
7.0.00.0170.04320.14
5.6.280.0000.07321.21
5.6.250.0030.08720.76
5.6.240.0070.08720.63
5.6.230.0170.07020.72
5.6.220.0200.07020.76
5.6.210.0230.06720.70
5.6.200.0030.05021.18
5.6.190.0030.09021.30
5.6.180.0170.07721.23
5.6.170.0030.09321.18
5.6.160.0030.04321.18
5.6.150.0100.08021.19
5.6.140.0100.04721.23
5.6.130.0100.07721.14
5.6.120.0100.08021.23
5.6.110.0070.05021.20
5.6.100.0100.07721.13
5.6.90.0070.08021.04
5.6.80.0070.08020.51
5.6.70.0030.08020.64
5.6.60.0100.05020.66
5.6.50.0030.06020.54
5.6.40.0100.07720.52
5.6.30.0100.05320.42
5.6.20.0030.04020.41
5.6.10.0070.03720.61
5.6.00.0030.04720.57
5.5.380.0070.08020.71
5.5.370.0100.08020.70
5.5.360.0070.05320.47
5.5.350.0070.08020.55
5.5.340.0030.07320.97
5.5.330.0070.06720.93
5.5.320.0130.07721.06
5.5.310.0030.08720.75
5.5.300.0070.08721.02
5.5.290.0070.08021.02
5.5.280.0100.08320.93
5.5.270.0100.07720.89
5.5.260.0100.07721.04
5.5.250.0070.09020.80
5.5.240.0130.04020.39
5.5.230.0070.08720.43
5.5.220.0000.07720.30
5.5.210.0070.04720.41
5.5.200.0100.08020.34
5.5.190.0130.03320.39
5.5.180.0000.06720.36
5.5.160.0070.04720.21
5.5.150.0070.03720.23
5.5.140.0000.04320.28
5.5.130.0070.03720.35
5.5.120.0070.03320.21
5.5.110.0070.03720.19
5.5.100.0130.02720.27
5.5.90.0100.07020.22
5.5.80.0030.04320.16
5.5.70.0030.04020.22
5.5.60.0000.04020.23
5.5.50.0000.04020.23
5.5.40.0030.05720.07
5.5.30.0030.03720.26
5.5.20.0070.06020.17
5.5.10.0070.06320.15
5.5.00.0070.04320.15
5.4.450.0000.08719.60
5.4.440.0100.07719.27
5.4.430.0030.06019.56
5.4.420.0070.07319.42
5.4.410.0100.08319.27
5.4.400.0130.07319.17
5.4.390.0100.07719.14
5.4.380.0030.04719.09
5.4.370.0070.05018.94
5.4.360.0000.04319.11
5.4.350.0070.04719.10
5.4.340.0100.05319.18
5.4.320.0030.04319.11
5.4.310.0000.04019.14
5.4.300.0070.04019.23
5.4.290.0030.03719.16
5.4.280.0000.04019.09
5.4.270.0070.03319.23
5.4.260.0030.04019.24
5.4.250.0000.04019.08
5.4.240.0030.07019.19
5.4.230.0070.03319.14
5.4.220.0100.05319.23
5.4.210.0030.03719.09
5.4.200.0000.04319.16
5.4.190.0000.08019.23
5.4.180.0030.03719.23
5.4.170.0030.03719.00
5.4.160.0070.03018.99
5.4.150.0030.04319.07
5.4.140.0100.02716.51
5.4.130.0030.03316.44
5.4.120.0000.03716.59
5.4.110.0100.02716.54
5.4.100.0030.04716.43
5.4.90.0030.03316.45
5.4.80.0030.07316.47
5.4.70.0030.03716.49
5.4.60.0100.03716.41
5.4.50.0030.03316.40
5.4.40.0030.03716.41
5.4.30.0030.03716.52
5.4.20.0000.03716.42
5.4.10.0000.06016.50
5.4.00.0070.05016.00

preferences:
51.9 ms | 401 KiB | 5 Q