3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function exceptionHandler($exception) { // these are our templates $traceline = "#%s %s(%s): %s(%s)"; $msg = "PHP Fatal error: Uncaught exception '%s' with message '%s' in %s:%s\nStack trace:\n%s\n thrown in %s on line %s"; // alter your trace as you please, here $trace = $exception->getTrace(); foreach ($trace as $key => $stackPoint) { // I'm converting arguments to their type // (prevents passwords from ever getting logged as anything other than 'string') $trace[$key]['args'] = array_map('gettype', $trace[$key]['args']); } // build your tracelines $result = array(); foreach ($trace as $key => $stackPoint) { $result[] = sprintf( $traceline, $key, $stackPoint['file'], $stackPoint['line'], $stackPoint['function'], implode(', ', $stackPoint['args']) ); } // trace always ends with {main} $result[] = '#' . ++$key . ' {main}'; // write tracelines into main template $msg = sprintf( $msg, get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine(), implode("\n", $result), $exception->getFile(), $exception->getLine() ); // log or echo as you please print $msg; } set_exception_handler("exceptionHandler"); a(1); function a($i) { throw new Exception("Just invoking the exception handler"); }
Output for 5.0.0 - 5.0.2, 5.0.5 - 7.1.0
PHP Fatal error: Uncaught exception 'Exception' with message 'Just invoking the exception handler' in /in/GIlJM:54 Stack trace: #0 /in/GIlJM(50): a(integer) #1 {main} thrown in /in/GIlJM on line 54
Output for 5.0.3 - 5.0.4
Notice: Undefined index: args in /in/GIlJM on line 14 Warning: array_map(): Argument #2 should be an array in /in/GIlJM on line 14 Warning: implode(): Bad arguments. in /in/GIlJM on line 26 PHP Fatal error: Uncaught exception 'Exception' with message 'Just invoking the exception handler' in /in/GIlJM:54 Stack trace: #0 /in/GIlJM(54): a() #1 /in/GIlJM(50): a(integer) #2 {main} thrown in /in/GIlJM on line 54
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_NEW in /in/GIlJM on line 54
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_NEW in /in/GIlJM on line 54
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/GIlJM on line 54
Process exited with code 255.