3v4l.org

run code in 300+ PHP versions simultaneously
<?php set_error_handler('error_handler'); // 無名関数によるコールバックは PHP5.3 から有効です function error_handler($errno, $errstr, $errfile, $errline) { // エラーの種別を定数から文字列化するための対応をまとめた連想配列 $errMap = array(// match式は PHP8 から、[]による配列宣言構文は PHP5.4 から有効です E_ERROR => 'ERROR', E_WARNING => 'WARNING', E_NOTICE => 'NOTICE', E_CORE_ERROR => 'CORE_ERROR', E_CORE_WARNING => 'CORE_WARNING', E_COMPILE_ERROR => 'COMPILE_ERROR', E_COMPILE_WARNING => 'COMPILE_WARNING', E_USER_ERROR => 'USER_ERROR', E_USER_WARNING => 'USER_WARNING', E_USER_NOTICE => 'USER_NOTICE', E_STRICT => 'STRICT', // E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR', // PHP5.2 から有効な定数です // E_DEPRECATED => 'DEPRECATED', // PHP5.3 から有効な定数です // E_USER_DEPRECATED => 'USER_DEPRECATED', // PHP5.3 から有効な定数です E_ALL => 'ALL', ); // 上記のエラー種別配列からエラーの種別を文字列化 // null 合体演算子は PHP7.0 から有効です $errLevel = isset($errMap[$errno]) ? $errMap[$errno] : 'UNKNOWN No.'.$errno; // エラーの内容を元に例外を投げます if (version_compare(PHP_VERSION, '5.1.0') >= 0) { // @see https://www.php.net/manual/ja/class.errorexception.php PHP: ErrorException - Manual // @see https://qiita.com/mpyw/items/c69da9589e72ceac470c お前は PHP 7 における Fatal Error / Catchable Fatal Error / Error / ErrorException / Exception の違いを言えるか? // PHP のエラーを例外に変換する時のための組み込み例外クラスのインスタンスを投げる throw new ErrorException("{$errLevel}: {$errstr} {$errfile} {$errline}", 0, $errno, $errfile, $errline); } elseif (version_compare(PHP_VERSION, '5.0.0') >= 0) { // PHP5.1 より前は ErrorException クラスが存在しないので代わりに Exception クラスを使う throw new Exception("{$errLevel}: {$errstr} {$errfile} {$errline}", 0, $errno, $errfile, $errline); } } // 例 echo $a; /* Fatal error: Uncaught ErrorException: WARNING: Undefined variable $a /in/ZBILA 32 in /in/ZBILA:32 Stack trace: #0 /in/ZBILA(32): error_handler(2, 'Undefined varia...', '/in/ZBILA', 32) #1 {main} thrown in /in/ZBILA on line 32 */
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught ErrorException: WARNING: Undefined variable $a /in/tGHtO 41 in /in/tGHtO:41 Stack trace: #0 /in/tGHtO(41): error_handler(2, 'Undefined varia...', '/in/tGHtO', 41) #1 {main} thrown in /in/tGHtO on line 41
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Fatal error: Uncaught ErrorException: NOTICE: Undefined variable: a /in/tGHtO 41 in /in/tGHtO:41 Stack trace: #0 /in/tGHtO(41): error_handler(8, 'Undefined varia...', '/in/tGHtO', 41, Array) #1 {main} thrown in /in/tGHtO on line 41
Process exited with code 255.
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Uncaught exception 'ErrorException' with message 'NOTICE: Undefined variable: a /in/tGHtO 41' in /in/tGHtO:41 Stack trace: #0 /in/tGHtO(41): error_handler(8, 'Undefined varia...', '/in/tGHtO', 41, Array) #1 {main} thrown in /in/tGHtO on line 41
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Fatal error: Uncaught exception 'ErrorException' with message 'NOTICE: Undefined variable: a /in/tGHtO 41' in /in/tGHtO:41 Stack trace: #0 /in/tGHtO(41): error_handler() #1 {main} thrown in /in/tGHtO on line 41
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: Wrong parameter count for exception([string $exception [, long $code ]]) in Unknown on line 0
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_NEW in /in/tGHtO on line 32
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
Parse error: parse error, unexpected T_NEW in /in/tGHtO on line 32
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/tGHtO on line 32
Process exited with code 255.

preferences:
278.83 ms | 401 KiB | 468 Q