3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Symfony\Component\Debug; if (!defined('ENT_SUBSTITUTE')) { define('ENT_SUBSTITUTE', 8); } class ExceptionHandler { private $debug; private $charset; private $handler; private $caughtOutput = 0; public function __construct($debug = true, $charset = 'UTF-8') { $this->debug = $debug; $this->charset = $charset; } public static function register($debug = true) { $handler = new static($debug); set_exception_handler(array($handler, 'handle')); return $handler; } public function setHandler($handler) { if (isset($handler) && !is_callable($handler)) { throw new \LogicException('The exception handler must be a valid PHP callable.'); } $old = $this->handler; $this->handler = $handler; return $old; } public function handle(\Exception $exception) { $caughtOutput = 0; $this->caughtOutput = false; ob_start(array($this, 'catchOutput')); try { echo $exception->getMessage(); } catch (\Exception $e) { // Ignore this $e exception, we have to deal with $exception } if (false === $this->caughtOutput) { ob_end_clean(); } if (isset($this->caughtOutput[0])) { ob_start(array($this, 'cleanOutput')); echo $this->caughtOutput; $caughtOutput = ob_get_length(); } $this->caughtOutput = 0; if (!empty($this->handler)) { try { call_user_func($this->handler, $exception); if ($caughtOutput) { $this->caughtOutput = $caughtOutput; } ob_end_flush(); } catch (\Exception $e) { if (!$caughtOutput) { // All handlers failed. Let PHP handle that now. throw $exception; } } } } public function catchOutput($buffer) { $this->caughtOutput = $buffer; return ''; } public function cleanOutput($buffer) { if ($this->caughtOutput) { // use substr_replace() instead of substr() for mbstring overloading resistance $cleanBuffer = substr_replace($buffer, '', 0, $this->caughtOutput); if (isset($cleanBuffer[0])) { $buffer = $cleanBuffer; } } return $buffer; } } ExceptionHandler::register(); throw new \Exception('foo msg');
Output for 5.3.0 - 5.3.29, 5.4.10 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
foo msg
Output for 5.4.0 - 5.4.9
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/FSnLL on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/FSnLL on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/FSnLL on line 3
Process exited with code 255.

preferences:
241.44 ms | 401 KiB | 456 Q