定義済み定数

以下の定数は、PHP コアに含まれており、常に利用可能です。

注意: 以下の定数をphp.iniで使用することができますが、 httpd.confのようなPHPの外部では、 代わりにビットマスク値を使用する必要があります。

エラーとロギング
定数 説明 注記
1 E_ERROR (int) 重大な実行時エラー。これは、メモリ確保に関する問題のように復帰で きないエラーを示します。スクリプトの実行は中断されます。
2 E_WARNING (int) 実行時の警告 (致命的なエラーではない)。スクリプトの実行は中断さ れません。  
4 E_PARSE (int) コンパイル時のパースエラー。パースエラーはパーサでのみ生成されま す。  
8 E_NOTICE (int) 実行時の警告。エラーを発しうる状況に遭遇したことを示す。 ただし通常のスクリプト実行の場合にもこの警告を発することがありうる。  
16 E_CORE_ERROR (int) PHPの初期始動時点での致命的なエラー。E_ERRORに 似ているがPHPのコアによって発行される点が違う。  
32 E_CORE_WARNING (int) (致命的ではない)警告。PHPの初期始動時に発生する。 E_WARNINGに似ているがPHPのコアによって発行される 点が違う。  
64 E_COMPILE_ERROR (int) コンパイル時の致命的なエラー。E_ERRORに 似ているがZendスクリプティングエンジンによって発行される点が違う。  
128 E_COMPILE_WARNING (int) コンパイル時の警告(致命的ではない)。E_WARNINGに 似ているがZendスクリプティングエンジンによって発行される点が違う。  
256 E_USER_ERROR (int) ユーザーによって発行されるエラーメッセージ。E_ERROR に似ているがPHPコード上でtrigger_error()関数を 使用した場合に発行される点が違う。  
512 E_USER_WARNING (int) ユーザーによって発行される警告メッセージ。E_WARNING に似ているがPHPコード上でtrigger_error()関数を 使用した場合に発行される点が違う。  
1024 E_USER_NOTICE (int) ユーザーによって発行される注意メッセージ。E_NOTICEに に似ているがPHPコード上でtrigger_error()関数を 使用した場合に発行される点が違う。  
2048 E_STRICT (int) コードの相互運用性や互換性を維持するために PHP がコードの変更を提案する。  
4096 E_RECOVERABLE_ERROR (int) キャッチできる致命的なエラー。危険なエラーが発生したが、 エンジンが不安定な状態になるほどではないことを表す。 ユーザー定義のハンドラでエラーがキャッチされなかった場合 (set_error_handler() も参照ください) は、 E_ERROR として異常終了する。  
8192 E_DEPRECATED (int) 実行時の注意。これを有効にすると、 将来のバージョンで動作しなくなるコードについての警告を受け取ることができる。  
16384 E_USER_DEPRECATED (int) ユーザー定義の警告メッセージ。これは E_DEPRECATED と同等だが、 PHP のコード上で関数 trigger_error() によって作成されるという点が異なる。  
32767 E_ALL (int) 全てのエラーと警告。  

上記の値(数値も論理値も)はどのエラーをレポートするかを指定する ビットマスクを組み立てる。ビット演算子 を使用して値を組み合わせたり特定のエラータイプをマスクすることができる。 php.ini では'|', '~', '!', '^' および '&'のみが解釈されることに 注意すべきである。

add a note

User Contributed Notes 9 notes

up
23
Andy at Azurite (co uk)
14 years ago
-1 is also semantically meaningless as a bit field, and only works in 2s-complement numeric representations.  On a 1s-complement system -1 would not set E_ERROR.  On a sign-magnitude system -1 would set nothing at all! (see e.g. http://en.wikipedia.org/wiki/Ones%27_complement)If you want to set all bits, ~0 is the correct way to do it.But setting undefined bits could result in undefined behaviour and that means *absolutely anything* could happen :-)
up
15
cl at viazenetti dot de
7 years ago
An other way to get all PHP errors  that are set to be reported. This code will even work, when additional error types are added in future.<?php$pot = 0;foreach (array_reverse(str_split(decbin(error_reporting()))) as $bit) {    if ($bit == 1) {        echo array_search(pow(2, $pot), get_defined_constants(true)['Core']). "<br>\n";    }    $pot++;}?>
up
18
russthom at fivegulf dot com
13 years ago
[Editor's note: fixed E_COMPILE_* cases that incorrectly returned E_CORE_* strings. Thanks josiebgoode.]

The following code expands on Vlad's code to show all the flags that are set.  if not set, a blank line shows.

<?php
$errLvl = error_reporting();
for ($i = 0; $i < 15;  $i++ ) {
    print FriendlyErrorType($errLvl & pow(2, $i)) . "<br>\\n"; 
}

function FriendlyErrorType($type)
{
    switch($type)
    {
        case E_ERROR: // 1 //
            return 'E_ERROR';
        case E_WARNING: // 2 //
            return 'E_WARNING';
        case E_PARSE: // 4 //
            return 'E_PARSE';
        case E_NOTICE: // 8 //
            return 'E_NOTICE';
        case E_CORE_ERROR: // 16 //
            return 'E_CORE_ERROR';
        case E_CORE_WARNING: // 32 //
            return 'E_CORE_WARNING';
        case E_COMPILE_ERROR: // 64 //
            return 'E_COMPILE_ERROR';
        case E_COMPILE_WARNING: // 128 //
            return 'E_COMPILE_WARNING';
        case E_USER_ERROR: // 256 //
            return 'E_USER_ERROR';
        case E_USER_WARNING: // 512 //
            return 'E_USER_WARNING';
        case E_USER_NOTICE: // 1024 //
            return 'E_USER_NOTICE';
        case E_STRICT: // 2048 //
            return 'E_STRICT';
        case E_RECOVERABLE_ERROR: // 4096 //
            return 'E_RECOVERABLE_ERROR';
        case E_DEPRECATED: // 8192 //
            return 'E_DEPRECATED';
        case E_USER_DEPRECATED: // 16384 //
            return 'E_USER_DEPRECATED';
    }
    return "";
}
?>
up
13
bbrokman at gmail dot com
6 years ago
A neat way to have a place in code to control error reporting configuration :)<?php$errorsActive = [    E_ERROR             => FALSE,    E_WARNING           => TRUE,    E_PARSE             => TRUE,    E_NOTICE            => TRUE,    E_CORE_ERROR        => FALSE,    E_CORE_WARNING      => FALSE,    E_COMPILE_ERROR     => FALSE,    E_COMPILE_WARNING   => FALSE,    E_USER_ERROR        => TRUE,    E_USER_WARNING      => TRUE,    E_USER_NOTICE       => TRUE,    E_STRICT            => FALSE,    E_RECOVERABLE_ERROR => TRUE,    E_DEPRECATED        => FALSE,    E_USER_DEPRECATED   => TRUE,    E_ALL               => FALSE,];error_reporting(    array_sum(        array_keys($errorsActive, $search = true)    ));?>
up
12
fadhilinjagi at gmail dot com
3 years ago
A simple and neat way to get the error level from the error code. You can even customize the error level names further.<?php$exceptions = [        E_ERROR => "E_ERROR",        E_WARNING => "E_WARNING",        E_PARSE => "E_PARSE",        E_NOTICE => "E_NOTICE",        E_CORE_ERROR => "E_CORE_ERROR",        E_CORE_WARNING => "E_CORE_WARNING",        E_COMPILE_ERROR => "E_COMPILE_ERROR",        E_COMPILE_WARNING => "E_COMPILE_WARNING",        E_USER_ERROR => "E_USER_ERROR",        E_USER_WARNING => "E_USER_WARNING",        E_USER_NOTICE => "E_USER_NOTICE",        E_STRICT => "E_STRICT",        E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",        E_DEPRECATED => "E_DEPRECATED",        E_USER_DEPRECATED => "E_USER_DEPRECATED",        E_ALL => "E_ALL"];echo $exceptions["1"];$code = 256;echo $exceptions[$code];?>Output:  E_ERRORE_USER_ERRORThis will need updating when PHP updates the error level names. Otherwise, it works just fine.
up
6
kezzyhko at NOSPAM dot semysha dot ru
9 years ago
As for me, the best way to get error name by int value is that. And it's works fine for me ;)<?phparray_flip(array_slice(get_defined_constants(true)['Core'], 1, 15, true))[$type];//the same in readable formarray_flip(    array_slice(        get_defined_constants(true)['Core'],        1,        15,        true    ))[$type]?>
up
2
kaioker
3 years ago
super simple error code to human readable conversion:function prettycode($code){    return $code == 0 ? "FATAL" : array_search($code, get_defined_constants(true)['Core']);}
up
-1
madcat34 at gmail dot com
5 months ago
Since PHP 8.4.0:E_STRICT (2048) has been removed.E_ALL is now equal to 30719
up
-1
ErikBachmann
6 years ago
<?php
function getErrorTypeByValue($type) {
    $constants  = get_defined_constants(true);

    foreach ( $constants['Core'] as $key => $value ) { // Each Core constant
        if ( preg_match('/^E_/', $key  ) ) {    // Check error constants
            if ( $type == $value ) 
                return( "$key=$value");
        }
    }
}   // getErrorTypeByValue() 

echo "[".getErrorTypeByValue( 1 ) . "]". PHP_EOL;
echo "[".getErrorTypeByValue( 0 ) . "]". PHP_EOL;
echo "[".getErrorTypeByValue( 8 ) . "]". PHP_EOL;
?>

Will give
    [E_ERROR=1]
    []
    [E_NOTICE=8]
To Top