3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php if(!defined('E_DEPRECATED')){ define('E_DEPRECATED', 0); } if(!defined('E_USER_DEPRECATED')){ define('E_USER_DEPRECATED', 0); } const EHS_ERRORS = array( E_ERROR => array('ERROR', 'Error'), E_WARNING => array('WARNING', 'Warning'), E_PARSE => array('PARSE', 'Parsing Error'), E_NOTICE => array('NOTICE', 'Notice'), E_CORE_ERROR => array('CORE_ERROR', 'Core Error'), E_CORE_WARNING => array('CORE_WARNING', 'Core Warning'), E_COMPILE_ERROR => array('COMPILE_ERROR', 'Compile Error'), E_COMPILE_WARNING => array('COMPILE_WARNING', 'Compile Warning'), E_USER_ERROR => array('USER_ERROR', 'Triggered Error'), E_USER_WARNING => array('USER_WARNING', 'Triggered Warning'), E_USER_NOTICE => array('USER_NOTICE', 'Triggered Notice'), E_STRICT => array('STRICT', 'Strict Standarts'), E_RECOVERABLE_ERROR => array('RECOVERABLE_ERROR', 'Catchable Fatal Error'), E_DEPRECATED => array('DEPRECATED', 'Deprecated'), E_USER_DEPRECATED => array('USER_DEPRECATED', 'Triggered Deprecated') ); define('EHS_CHECK_TOKENS_RGX', "/\(T_(".implode('|', array_keys(EHS_TOKENS)).")\)/"); define('EHS_NL', "<br>"); define('EHS_OPEN', "<div style='padding:1px;margin:1px 0;white-space:pre-wrap;border:1px solid black;word-wrap:break-word;'>"); define('EHS_SOURCE_OPEN', "<div style='padding:1px;margin:1px 0 0;white-space:pre-wrap;border:1px solid black;word-wrap:break-word;tab-size:2;font-family:monospace;'>"); define('EHS_CLOSE', "</div>"); define('EHS_OFF', 1); define('EHS_GET_BACKTRACE', 2); define('EHS_GET_SOURCE', 4); define('EHS_GET_TOKEN_EXAMPLE',8); define('EHS_GET_TOKEN_INFO', 16); define('EHS_GET_MEMORY_USAGE', 32); define('EHS_DEBUG', 64); define('EHS_MUST_IGNORE_REPORTING', 128); define('EHS_ALL', (4096 - 1) ^ EHS_OFF); define('EHS_UTF', (extension_loaded('mbstring') ? (ini_get('mbstring.internal_encoding') == 'UTF-8') : false)); class EHS { public static $previousAssertOptions = array(); public static $instance = null; public $maxchars = 30; public $linkFormat = ''; public $mask = 0; public function __construct($mask = EHS_OFF){ if(self::$instance != null){ throw new Exception('EHS Object already exists, check EHS::$instance'); } $this->mask = $mask; self::$instance = &$this; } public function reporting($E = null){ if($E === null){ return error_reporting(); }else{ error_reporting($E); } return $this; } public function displayAllErrors(){ $this->reporting(E_ALL | E_STRICT); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); return $this; } public function setup($error = false, $fatal = false, $exception = false, $assert = false){ if($error){ set_error_handler(array($this, 'ErrorHandler')); } if($fatal){ register_shutdown_function(array($this, 'FatalErrorHandler')); ob_start(); } if($exception){ set_exception_handler(array($this, 'ExceptionHandler')); } if($assert){ self::$previousAssertOptions[ASSERT_ACTIVE] = assert_options(ASSERT_ACTIVE); self::$previousAssertOptions[ASSERT_WARNING] = assert_options(ASSERT_ACTIVE); self::$previousAssertOptions[ASSERT_BAIL] = assert_options(ASSERT_BAIL); self::$previousAssertOptions[ASSERT_QUIET_EVAL] = assert_options(ASSERT_QUIET_EVAL); self::$previousAssertOptions[ASSERT_CALLBACK] = assert_options(ASSERT_CALLBACK); assert_options(ASSERT_ACTIVE, 1); assert_options(ASSERT_WARNING, 0); assert_options(ASSERT_BAIL, 0); assert_options(ASSERT_QUIET_EVAL, 0); assert_options(ASSERT_CALLBACK, array($this, 'AssertionHandler')); } return $this; } public function setupEnvironment(){ ini_set('html_errors', 'On'); ini_set('docref_root', ''); ini_set('docref_ext', ''); ini_set('log_errors', 'On'); ini_set('log_errors_max_len', 0); ini_set('ignore_repeated_errors', 'Off'); ini_set('ignore_repeated_source', 'Off'); ini_set('report_memleaks', 'Off'); ini_set('track_errors', 'On'); ini_set('xmlrpc_errors', 'Off'); ini_set('xmlrpc_error_number', 'Off'); ini_set('error_prepend_string', ''); ini_set('error_append_string', ''); return $this; } public function restore($error = false, $exception = false, $assert = false){ if($error){ restore_error_handler(); } if($exception){ restore_exception_handler(); } if($assert){ assert_options(ASSERT_ACTIVE, self::$previousAssertOptions[ASSERT_ACTIVE]); assert_options(ASSERT_WARNING, self::$previousAssertOptions[ASSERT_WARNING]); assert_options(ASSERT_BAIL, self::$previousAssertOptions[ASSERT_BAIL]); assert_options(ASSERT_QUIET_EVAL, self::$previousAssertOptions[ASSERT_QUIET_EVAL]); assert_options(ASSERT_CALLBACK, self::$previousAssertOptions[ASSERT_CALLBACK]); } return $this; } public function ErrorHandler($code, $message, $file, $line, $context = INF){ if(($this->mask & EHS_OFF) || ($ignored = $this->ignoreReporting($code)) == 'X'){ return false; }else{ $debug = ($this->mask & EHS_DEBUG) || !($code & (E_NOTICE | E_USER_NOTICE | E_DEPRECATED | E_USER_DEPRECATED | E_STRICT)); $types = EHS_ERRORS[$code]; $text = EHS_OPEN; $text .= "<b>[".date('d-M-Y H:i:s')."] $ignored [E_$types[0]]".(($this->mask & EHS_GET_MEMORY_USAGE) && $debug ? ' [Memory Usage: '.memory_get_usage().' Bytes]' : '')."</b>".EHS_NL; $text .= "<b>$types[1]:</b> {$message}".EHS_NL; $text .= " in <b>[{$this->formatLink($file, $line)}]</b>".EHS_NL; if($debug){ $text .= $this->getErrorSourceCode($file, $line); if($this->mask & EHS_GET_BACKTRACE){ $text .= $this->formatBacktrace($this->backtrace(), $context === INF ? 3 : 2); } } echo $text.EHS_CLOSE; return true; } } public function FatalErrorHandler(){ $error = error_get_last(); if($error && $error['type'] & (E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR)){ while(ob_get_level()){ ob_end_clean(); } $this->ErrorHandler($error['type'], $error['message'], $error['file'], $error['line']); exit; }else{ ob_end_flush(); } } public function ExceptionHandler($e){ $hasChain = !!$e->getPrevious(); $text = ($hasChain ? EHS_OPEN : ''); $date = date('d-M-Y H:i:s'); do{ if($e instanceof ErrorException){ $text .= $this->ErrorException($e, $date); }else{ $text .= $this->Exception($e, $date); } }while($e = $e->getPrevious()); $text .= ($hasChain ? EHS_CLOSE : ''); echo $text; } protected function ErrorException($e, $date){ $code = $e->getCode(); $file = $e->getFile(); $line = $e->getLine(); $severity = $e->getSeverity(); $types = EHS_ERRORS[$severity] ?: array('UNKNOWN', 'Unknown'); $text = EHS_OPEN; $text .= "<b>[$date] [E_$types[0]]".($code ? " [Code: $code]" : '')."</b>".EHS_NL; $text .= "<b>$types[1] Exception:</b> {$e->getMessage()}".EHS_NL; $text .= " thrown in <b>[{$this->formatLink($file, $line)}]</b>".EHS_NL; $text .= $this->getErrorSourceCode($file, $line); $text .= $this->formatBacktrace($e->getTrace()); $text .= EHS_CLOSE; return $text; } protected function Exception($e, $date){ $code = $e->getCode(); $file = $e->getFile(); $line = $e->getLine(); $text = EHS_OPEN; $text .= "<b>[$date]".($code ? " [Code: $code]" : '')."</b>".EHS_NL; $text .= "<b>".get_class($e).":</b> {$e->getMessage()}".EHS_NL; $text .= " thrown in <b>[{$this->formatLink($file, $line)}]</b>".EHS_NL; $text .= $this->getErrorSourceCode($file, $line); $text .= $this->formatBacktrace($e->getTrace()); $text .= EHS_CLOSE; return $text; } public function AssertionHandler($file, $line, $code, $message = ''){ $text = EHS_OPEN; $text .= "<b>[".date('d-M-Y H:i:s')."]</b>".EHS_NL; $text .= "<b>Assertion Failed:</b> $message".($code ? " ($code)" : '').EHS_NL; $text .= " in <b>[{$this->formatLink($file, $line)}]</b>".EHS_NL; echo $text.EHS_CLOSE; return true; } protected function formatBacktrace($trace, $init = 0){ $length = count($trace); if($length > $init){ $text = EHS_SOURCE_OPEN.'Trace:'; for($i = $init; $i < $length; $i++){ $point = $trace[$i]; $text .= EHS_NL.' \–––> '; $args = isset($point['args']) ? $this->formatArgs($point['args']) : '()'; $text .= isset($point['class']) ? $point['class'] : ''; $text .= isset($point['type']) ? $point['type'] : ''; $text .= isset($point['function']) ? $point['function'].$args : '???'; $text .= EHS_NL.' at ['.(isset($point['file']) ? $this->formatLink($point['file'], (isset($point['line']) ? $point['line'] : 0)) : 'unknown').']'; } unset($trace); return $text.EHS_CLOSE; }else{ return ''; } } protected function getErrorSourceCode($file, $line){ if(($this->mask & EHS_GET_SOURCE) && $file && $line && ($lines = file($file)) !== false){ $max_length = $this->stringLength((string) ($line + 1)); $compile = function($num, $line) use ($max_length){ $out = $num.str_repeat(' ', $max_length - $this->stringLength((string) $num))." | ".htmlspecialchars($line); return $out; }; $text = EHS_SOURCE_OPEN; $text .= ($lines[$line - 2] ? $compile($line - 1, $lines[$line - 2]) : ''); $text .= "<font style='display:block;background:rgba(255, 165, 165, 0.3);'>".$compile($line, $lines[$line - 1])."</font>"; $text .= ($lines[$line] ? $compile($line + 1, $lines[$line]) : ''); return $text.EHS_CLOSE; }else{ return ''; } } protected function ignoreReporting($code){ if(!(error_reporting() & $code)){ if($this->mask & EHS_MUST_IGNORE_REPORTING){ return '@'; }else{ return 'X'; } } return ''; } protected function formatLink($file, $line){ if(!$file || !$this->linkFormat){ return "{$this->shortFile($file)}:$line"; } return "<a href='".str_replace(array('%f', '%l'), array(urlencode($file), $line), $this->linkFormat)."' style='color:black;'>{$this->shortFile($file)}:$line</a>"; } protected function shortFile($file){ return str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', $file)); } protected function backtrace(){ if(defined('DEBUG_BACKTRACE_PROVIDE_OBJECT')){ return debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT); }else{ return debug_backtrace(true); } } protected function formatString($str){ if($this->stringLength($str) > $this->maxchars){ if(EHS_UTF){ $str = trim(mb_substr($str, 0, $this->maxchars / 2)).'…'.trim(mb_substr($str, -$this->maxchars / 2)); }else{ $str = substr($str, 0, $this->maxchars / 2).'...'.substr($str, -$this->maxchars / 2); } } return $str; } protected function stringLength($str){ if(EHS_UTF){ $strlen = mb_strlen($str); }else{ $strlen = strlen($str); } return $strlen; } protected function formatArg($arg){ switch(gettype($arg)){ case 'boolean': return $arg ? 'true' : 'false'; case 'NULL': return 'null'; case 'integer': case 'double': case 'float': $arg = (string) $arg; if(EHS_UTF){ $arg = str_replace('INF', '∞', $arg); }else{ $arg = str_replace('INF', 'Infinity', $arg); } $arg = str_replace('NAN', 'NaN', $arg); return $arg; case 'string': if(is_callable($arg, false, $name)){ return "fs:$name"; }else if(class_exists($arg, false)){ return "c:$arg"; }else if(interface_exists($arg, false)){ return "i:$arg"; }else if(function_exists('trait_exists') && trait_exists($arg, false)){ return "t:$arg"; }else{ $strlen = $this->stringLength($arg); $arg = $this->formatString($arg); if($strlen <= $this->maxchars){ $arg = "\"$arg\""; }else{ $arg = "\"$arg\"($strlen)"; } return str_replace("\n", '\n', str_replace("\t", '\t', $arg)); } case 'array': if(is_callable($arg, false, $name)){ return "fa:$name"; }else{ return 'array('.count($arg).')'; } case 'object': $object = get_class($arg).'():'.spl_object_hash($arg); if(is_callable($arg, false)){ $object = "fo:".$object; } return $object; case 'resource': return 'r:'.get_resource_type($arg); default: return 'unknown'; } } protected function formatArgs($args){ foreach($args as &$value){ $value = $this->formatArg($value); } return '('.implode(', ', $args).')'; } } ?>
based on SBRIL
Output for 7.2.0 - 7.3.1
Warning: Use of undefined constant EHS_TOKENS - assumed 'EHS_TOKENS' (this will throw an Error in a future version of PHP) in /in/spe7i on line 26 Warning: array_keys() expects parameter 1 to be array, string given in /in/spe7i on line 26 Warning: implode(): Invalid arguments passed in /in/spe7i on line 26
Output for 5.6.0 - 5.6.38, 7.0.33 - 7.1.25
Notice: Use of undefined constant EHS_TOKENS - assumed 'EHS_TOKENS' in /in/spe7i on line 26 Warning: array_keys() expects parameter 1 to be array, string given in /in/spe7i on line 26 Warning: implode(): Invalid arguments passed in /in/spe7i on line 26
Output for hhvm-3.22.0
Notice: Use of undefined constant EHS_TOKENS - assumed 'EHS_TOKENS' in /in/spe7i on line -1 Warning: array_keys() expects parameter 1 to be an array or collection in /in/spe7i on line -1 Warning: Invalid operand type was used: implode() expects a container as one of the arguments in /in/spe7i on line -1
Output for hhvm-3.21.3
Notice: Use of undefined constant EHS_TOKENS - assumed 'EHS_TOKENS' in /in/spe7i on line 26 Warning: array_keys() expects parameter 1 to be an array or collection in /in/spe7i on line 26 Warning: Invalid operand type was used: implode() expects a container as one of the arguments in /in/spe7i on line 26
Output for hhvm-3.18.5
Warning: Constants may only evaluate to scalar values in /in/spe7i on line 25 Notice: Use of undefined constant EHS_TOKENS - assumed 'EHS_TOKENS' in /in/spe7i on line 26 Warning: array_keys() expects parameter 1 to be an array or collection in /in/spe7i on line 26 Warning: Invalid operand type was used: implode() expects a container as one of the arguments in /in/spe7i on line 26