3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ExceptionFormatter { /** @var \Exception|\Throwable */ private $exception; /** @var string */ private $formattedString; /** * @param \Exception|\Throwable $exception */ private function __construct($exception) { $this->exception = $exception; $this->formattedString = $this->formatException(); } private function formatException() { return $this->formatExceptionMessage() .$this->formatExceptionTrace() .$this->getCauseIfApplicable(); } private function formatExceptionMessage() { $exceptionClass = get_class($this->exception); $exceptionMessage = $this->exception->getMessage(); $fileAndLine = $this->formatFileAndLine($this->exception->getFile(), $this->exception->getLine()); if ($exceptionMessage === '') return "${exceptionClass} (${fileAndLine})\n"; return "${exceptionClass}: ${exceptionMessage} (${fileAndLine})\n"; } private function formatFileAndLine($file, $line) { return "${file}:${line}"; } private function formatExceptionTrace() { $exceptionTrace = $this->exception->getTrace(); $formattedTrace = []; foreach($exceptionTrace as $trace) { $formattedTrace[] = "\tat ".$this->formatTraceElement($trace); } return implode("\n", $formattedTrace); } private function formatTraceElement($traceElement) { $fileAndLine = $this->formatFileAndLine( isset($traceElement['file']) ? $traceElement['file'] : 'unknown', isset($traceElement['line']) ? $traceElement['line'] : 'unknown' ); if ($this->isFunctionCall($traceElement)) { $functionCall = $this->formatFunctionCall($traceElement); $arguments = $this->formatArguments($traceElement); return "${functionCall}(${arguments}) (${fileAndLine})"; } return $fileAndLine; } private function isFunctionCall($traceElement) { return array_key_exists('function', $traceElement); } private function formatFunctionCall($traceElement) { return (isset($traceElement['class']) ? $traceElement['class'] : '') .(isset($traceElement['type']) ? $traceElement['type'] : '') .$traceElement['function']; } private function formatArguments($traceElement) { /** @var string[] $arguments */ $arguments = $traceElement['args']; $formattedArgs = []; foreach ($arguments as $arg) { $formattedArgs[] = $this->formatArgument($arg); } return implode(', ', $formattedArgs); } private function formatArgument($arg) { if (is_string($arg)) { return "\"".$arg."\""; } else if (is_array($arg)) { return 'Array'; } else if ($arg === null) { return 'null'; } else if (is_bool($arg)) { return $arg ? 'true' : 'false'; } else if (is_object($arg)) { return get_class($arg); } else if (is_resource($arg)) { return get_resource_type($arg); } else { return $arg; } } private function getCauseIfApplicable() { $previousException = $this->exception->getPrevious(); if ($previousException !== null) return "\nCaused by: " . self::format($previousException); return ''; } /** * Converts an Exception to a Java-style stack trace string. * * @param \Exception|\Throwable The Exception/Throwable to format as a "pretty" string. * @return string */ public static function format($exception) { $formatter = new ExceptionFormatter($exception); return $formatter->getFormattedString(); } public function getFormattedString() { return $this->formattedString; } } function throwCause() { throw new RuntimeException('This is the cause'); } function nestedFunction() { try { throwCause(); } catch (Exception $e) { throw new LogicException('Lulz!', -1, $e); } } function throwExceptionForLulz($a, $b, $c, $d, ...$e) { nestedFunction(); } try { throwExceptionForLulz(['a', 'c'], "String", new stdClass(), false, 'a', 'b'); } catch (Exception $e) { echo ExceptionFormatter::format($e); }

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.4.140.0100.01218.00
8.4.130.0120.00917.91
8.4.120.0030.00620.92
8.4.110.0040.00518.86
8.4.100.0120.00920.57
8.4.90.0050.00520.55
8.4.80.0110.00820.26
8.4.70.0080.01317.91
8.4.60.0100.01118.95
8.4.50.0120.00818.48
8.4.40.0100.01019.71
8.4.30.0110.00819.54
8.4.20.0100.00719.57
8.4.10.0050.00520.71
8.3.270.0120.00916.74
8.3.260.0120.00716.96
8.3.250.0100.00118.93
8.3.240.0130.00716.59
8.3.230.0080.00817.18
8.3.220.0120.00718.88
8.3.210.0110.00817.02
8.3.200.0060.00516.99
8.3.190.0110.00820.98
8.3.180.0040.00417.11
8.3.170.0070.01117.04
8.3.160.0160.00618.64
8.3.150.0190.00018.63
8.3.140.0060.00317.30
8.3.130.0000.00818.52
8.3.120.0060.00318.92
8.3.110.0090.00020.94
8.3.100.0100.00016.85
8.3.90.0050.00326.77
8.3.80.0030.00617.00
8.3.70.0060.01216.50
8.3.60.0110.00716.88
8.3.50.0160.00721.32
8.3.40.0100.01019.12
8.3.30.0090.00618.84
8.3.20.0040.00418.73
8.3.10.0040.00421.77
8.3.00.0050.00321.75
8.2.290.0120.00720.55
8.2.280.0060.00418.30
8.2.270.0130.00717.37
8.2.260.0120.00616.80
8.2.250.0080.00016.83
8.2.240.0060.00319.21
8.2.230.0030.00622.58
8.2.220.0000.00824.06
8.2.210.0070.01326.77
8.2.200.0030.00716.63
8.2.190.0160.00316.75
8.2.180.0120.00916.63
8.2.170.0100.00719.06
8.2.160.0110.00422.96
8.2.150.0050.00325.66
8.2.140.0090.00024.66
8.2.130.0080.00019.70
8.2.120.0040.00426.35
8.2.110.0030.00719.87
8.2.100.0100.00818.57
8.2.90.0040.00518.65
8.2.80.0040.00618.40
8.2.70.0040.00618.42
8.2.60.0110.00319.96
8.2.50.0090.00219.35
8.2.40.0040.00620.60
8.2.30.0040.00520.76
8.2.20.0070.00219.40
8.2.10.0050.00419.54
8.2.00.0040.00619.35
8.1.330.0120.00721.91
8.1.320.0110.00816.16
8.1.310.0150.00018.57
8.1.300.0060.00319.93
8.1.290.0090.00030.84
8.1.280.0110.00725.92
8.1.270.0080.00023.99
8.1.260.0040.00428.09
8.1.250.0060.00328.09
8.1.240.0070.00320.06
8.1.230.0050.00519.98
8.1.220.0050.00418.30
8.1.210.0080.00519.47
8.1.200.0010.00818.06
8.1.190.0040.00617.93
8.1.180.0020.01018.39
8.1.170.0050.00418.58
8.1.160.0070.00220.54
8.1.150.0030.00619.72
8.1.140.0030.00619.13
8.1.130.0050.00418.18
8.1.120.0070.00218.16
8.1.110.0040.00518.08
8.1.100.0050.00518.13
8.1.90.0060.00318.04
8.1.80.0030.00518.07
8.1.70.0030.00518.20
8.1.60.0060.00318.21
8.1.50.0060.00318.12
8.1.40.0040.00619.04
8.1.30.0090.00019.16
8.1.20.0050.00419.08
8.1.10.0020.00718.11
8.1.00.0020.00718.20
8.0.300.0070.00218.89
8.0.290.0030.00517.50
8.0.280.0040.00518.30
8.0.270.0020.00717.66
8.0.260.0050.01017.72
8.0.250.0080.00017.60
8.0.240.0030.00517.61
8.0.230.0020.00717.70
8.0.220.0080.00617.46
8.0.210.0070.00217.49
8.0.200.0050.00317.61
8.0.190.0050.00417.57
8.0.180.0080.00617.43
8.0.170.0020.00717.55
8.0.160.0040.00517.44
8.0.150.0020.00717.54
8.0.140.0050.00417.46
8.0.130.0070.00015.55
8.0.120.0090.00017.41
8.0.110.0030.00517.64
8.0.100.0070.00117.49
8.0.90.0040.00518.31
8.0.80.0050.00517.66
8.0.70.0040.00517.53
8.0.60.0050.00417.41
8.0.50.0030.00617.45
8.0.30.0090.00717.35
8.0.20.0130.01217.56
8.0.10.0060.00317.56
8.0.00.0050.01119.43
7.4.330.0030.00314.87
7.4.320.0040.00417.08
7.4.300.0030.00517.10
7.4.290.0080.00217.14
7.4.280.0080.00117.19
7.4.270.0040.00516.91
7.4.260.0060.00216.90
7.4.250.0030.00517.11
7.4.240.0040.00416.90
7.4.230.0020.00717.21
7.4.220.0080.00517.08
7.4.210.0040.00817.21
7.4.200.0020.00617.96
7.4.190.0090.00019.52
7.4.180.0080.00019.54
7.4.160.0060.00817.59
7.4.150.0080.00517.50
7.4.140.0080.00717.75
7.4.130.0060.01116.78
7.4.120.0130.00517.58
7.4.110.0050.01116.97
7.4.100.0140.00617.08
7.4.90.0120.00617.00
7.4.80.0110.00618.45
7.4.70.0150.00317.86
7.4.60.0130.00517.04
7.4.50.0130.00516.96
7.4.40.0110.00816.93
7.4.30.0120.00517.05
7.4.20.0180.00017.26
7.4.10.0150.00417.40
7.4.00.0080.00416.29
7.3.330.0080.00015.44
7.3.320.0080.00515.25
7.3.310.0090.00316.86
7.3.300.0100.00316.89
7.3.290.0090.00716.68
7.3.280.0080.01016.68
7.3.270.0130.00717.37
7.3.260.0110.00916.79
7.3.250.0100.01116.76
7.3.240.0140.00716.77
7.3.230.0140.00516.78
7.3.220.0190.00017.49
7.3.210.0160.01017.12
7.3.200.0120.00917.18
7.3.190.0220.00517.04
7.3.180.0190.01016.98
7.3.170.0170.01316.97
7.3.160.0200.01016.94
7.3.150.0320.00017.40
7.3.140.0170.00317.40
7.3.130.0090.00917.11
7.3.120.0160.00216.07
7.3.110.0090.00816.17
7.3.100.0100.00516.06
7.3.90.0090.00416.21
7.3.80.0120.00416.21
7.3.70.0080.00916.16
7.3.60.0080.00516.25
7.3.50.0070.00916.13
7.3.40.0130.00516.14
7.3.30.0220.00316.09
7.3.20.0330.00816.88
7.3.10.0300.00816.74
7.3.00.0230.01116.94
7.2.340.0030.00619.10
7.2.330.0150.00317.26
7.2.320.0120.00616.90
7.2.310.0090.00917.18
7.2.300.0160.00417.09
7.2.290.0100.00817.21
7.2.280.0210.01317.49
7.2.270.0240.00817.43
7.2.260.0160.00417.29
7.2.250.0150.00417.56
7.2.240.0140.00516.26
7.2.230.0090.00716.18
7.2.220.0100.00516.05
7.2.210.0100.00516.39
7.2.200.0120.00616.47
7.2.190.0080.00616.35
7.2.180.0170.00616.32
7.2.170.0170.00716.31
7.2.160.0040.01416.22
7.2.150.0370.00817.09
7.2.140.0640.01017.19
7.2.130.0290.00617.11
7.2.120.0280.00717.17
7.2.110.0290.00717.15
7.2.100.0200.00917.17
7.2.90.0250.00617.14
7.2.80.0230.00717.18
7.2.70.0200.00617.11
7.2.60.0210.00717.09
7.2.50.0190.00817.08
7.2.40.0250.00617.19
7.2.30.0250.00717.20
7.2.20.0260.00517.05
7.2.10.0260.00917.19
7.2.00.0230.01117.12
7.1.330.0060.01116.98
7.1.320.0140.00816.94
7.1.310.0130.00817.07
7.1.300.0140.00317.02
7.1.290.0120.00616.90
7.1.280.0200.00617.03
7.1.270.0140.01316.95
7.1.260.0150.01016.90
7.1.250.0290.00915.93
7.1.240.0530.00816.14
7.1.230.0470.00915.95
7.1.220.0440.00915.93
7.1.210.0370.00316.04
7.1.200.0300.00715.78
7.1.190.0430.00516.09
7.1.180.0420.00516.04
7.1.170.0440.00215.99
7.1.160.0420.00416.03
7.1.150.0480.00916.17
7.1.140.0550.00515.91
7.1.130.0470.00916.03
7.1.120.0530.00516.30
7.1.110.0600.00916.15
7.1.100.0470.00916.30
7.1.90.0520.00516.03
7.1.80.0660.00416.11
7.1.70.0300.01216.32
7.1.60.0330.00916.35
7.1.50.0410.00615.99
7.1.40.0380.00515.94
7.1.30.0460.00516.02
7.1.20.0400.00316.15
7.1.10.0440.01016.21
7.1.00.0290.02318.14
7.0.330.0570.00315.86
7.0.320.0720.00315.63
7.0.310.0510.00815.56
7.0.300.0490.01215.78
7.0.290.0340.00815.84
7.0.280.0440.01115.78
7.0.270.0540.00815.94
7.0.260.0500.00915.76
7.0.250.0560.00616.04
7.0.240.0490.00915.78
7.0.230.0640.00515.90
7.0.220.0700.00915.94
7.0.210.0470.00715.94
7.0.200.0400.00815.44
7.0.190.0440.00515.84
7.0.180.0470.00515.76
7.0.170.0530.01115.78
7.0.160.0520.00615.82
7.0.150.0420.00815.83
7.0.140.0320.00515.90
7.0.130.0340.01115.81
7.0.120.0390.02617.20
7.0.110.0340.02817.26
7.0.100.0360.02617.29
7.0.90.0410.02717.85
7.0.80.0350.02917.79
7.0.70.0310.03217.85
7.0.60.0380.02817.78
7.0.50.0260.02617.90
7.0.40.0250.02017.95
7.0.30.0340.02717.92
7.0.20.0310.02217.80
7.0.10.0320.01717.87
7.0.00.0320.02017.81
5.6.400.0290.00316.89
5.6.390.0170.00717.07
5.6.380.0180.00715.57
5.6.370.0200.00815.83
5.6.360.0240.00815.85
5.6.350.0270.00815.78
5.6.340.0180.00916.03
5.6.330.0150.01115.96
5.6.320.0200.00715.73
5.6.310.0130.01215.95
5.6.300.0120.01015.73
5.6.290.0160.00615.84
5.6.280.0230.00315.81
5.6.270.0130.02817.48
5.6.260.0160.03217.56
5.6.250.0160.03517.54
5.6.240.0150.03017.58
5.6.230.0090.03717.54
5.6.220.0180.03017.38
5.6.210.0140.02817.61
5.6.200.0180.02117.56
5.6.190.0130.02517.52
5.6.180.0130.02417.51
5.6.170.0190.02117.42
5.6.160.0130.02717.44
5.6.150.0100.02617.43
5.6.140.0160.02617.40
5.6.130.0170.02017.42
5.6.120.0110.02917.48
5.6.110.0060.03917.28
5.6.100.0160.02917.40
5.6.90.0120.03017.38
5.6.80.0130.02617.28
5.6.70.0120.02217.23
5.6.60.0110.02417.39
5.6.50.0090.02717.21
5.6.40.0070.02617.17
5.6.30.0090.02717.06
5.6.20.0080.02517.14
5.6.10.0080.02317.05
5.6.00.0070.03017.19

preferences:
37.08 ms | 403 KiB | 5 Q