trigger_error

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

trigger_error β€” Π“Π΅Π½Π΅Ρ€ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΏΡ€Π΅Π΄ΡƒΠΏΡ€Π΅ΠΆΠ΄Π΅Π½ΠΈΠ΅, ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ ΠΈΠ»ΠΈ сообщСниС ΠΎΠ± ошибкС Π½Π° ΡƒΡ€ΠΎΠ²Π½Π΅ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ

ОписаниС

function trigger_error(string $message, int $error_level = E_USER_NOTICE): true

Ѐункция ΠΈΠ½ΠΈΡ†ΠΈΠΈΡ€ΡƒΠ΅Ρ‚ состояниС ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΎΠΉ ошибки. Π€ΡƒΠ½ΠΊΡ†ΠΈΡŽ Ρ€Π°Π·Ρ€Π΅ΡˆΠ°Π΅Ρ‚ΡΡ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ Π² сочСтании со встроСнным ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠΌ ошибок ΠΈΠ»ΠΈ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ, ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠ» ΠΈ установил Ρ‡Π΅Ρ€Π΅Π· Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ set_error_handler() ΠΊΠ°ΠΊ Π½ΠΎΠ²Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибок.

Польза Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ проявляСтся ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΊΠΎΠ΄Π°, ΠΊΠΎΠ³Π΄Π° трСбуСтся ΡΠ³Π΅Π½Π΅Ρ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½Ρ‹ΠΉ ΠΎΡ‚Π²Π΅Ρ‚ Π½Π° ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

message

Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ функция Π½Π°Π·Π½Π°Ρ‡ΠΈΡ‚ ошибкС. Π”Π»ΠΈΠ½Π° сообщСния ограничиваСтся 1024 Π±Π°ΠΉΡ‚Π°ΠΌΠΈ. Ѐункция ΠΎΠ±Ρ€Π΅ΠΆΠ΅Ρ‚ символы, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΈΠ΄ΡƒΡ‚ послС 1024-Π³ΠΎ.

error_level

Π’ΠΈΠΏ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ функция Π½Π°Π·Π½Π°Ρ‡ΠΈΡ‚ ошибкС. ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ с константами сСмСйства E_USER_*, Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ Ρ€Π°Π²Π½ΠΎ E_USER_NOTICE.

Π’Π½ΠΈΠΌΠ°Π½ΠΈΠ΅

ΠŸΠ΅Ρ€Π΅Π΄Π°Ρ‡Π° константы E_USER_ERROR ΠΊΠ°ΠΊ значСния уровня ошибки Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ error_level устарСла. ВмСсто этого Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄ΡƒΡŽΡ‚ Π²Ρ‹Π±Ρ€Π°ΡΡ‹Π²Π°Ρ‚ΡŒ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Exception ΠΈΠ»ΠΈ Π²Ρ‹Π·Ρ‹Π²Π°Ρ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ exit().

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ логичСскоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true.

Ошибки

Ѐункция выбрасываСт ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ValueError, Ссли Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° error_level Π½Π΅ соотвСтствуСт значСниям E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE ΠΈΠ»ΠΈ E_USER_DEPRECATED.

Бписок измСнСний

ВСрсия ОписаниС
8.4.0 ΠŸΠ΅Ρ€Π΅Π΄Π°Ρ‡Π° константы E_USER_ERROR ΠΊΠ°ΠΊ значСния уровня ошибки Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ error_level устарСла. ВмСсто этого Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄ΡƒΡŽΡ‚ Π²Ρ‹Π±Ρ€Π°ΡΡ‹Π²Π°Ρ‚ΡŒ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Exception ΠΈΠ»ΠΈ Π²Ρ‹Π·Ρ‹Π²Π°Ρ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ exit().
8.4.0 ВмСсто Ρ‚ΠΈΠΏΠ° bool Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Ρ‚ΠΈΠΏ true.
8.0.0 Ѐункция выбрасываСт ΠΎΡˆΠΈΠ±ΠΊΡƒ ValueError, Ссли Π² Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π΅ error_level ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈ нСдопустимоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅. РаньшС функция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π»Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ false.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ запуска состояния ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΎΠΉ ошибки Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ trigger_error()

Π Π°Π·Π²Ρ‘Ρ€Π½ΡƒΡ‚Ρ‹ΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΏΡ€ΠΈΠ²ΠΎΠ΄ΠΈΡ‚ описаниС Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ set_error_handler().

<?php

if ($divisor == 0) {
trigger_error("НС ΠΌΠΎΠ³Ρƒ ΠΏΠΎΠ΄Π΅Π»ΠΈΡ‚ΡŒ Π½Π° ноль", E_USER_ERROR);
}

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π’Π½ΠΈΠΌΠ°Π½ΠΈΠ΅

HTML-сущности Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π΅ message Π½Π΅ ΡΠΊΡ€Π°Π½ΠΈΡ€ΡƒΡŽΡ‚ΡΡ. Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Ρ‹Π²Π°ΡŽΡ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ htmlentities(), Ссли ΠΎΡˆΠΈΠ±ΠΊΡƒ трСбуСтся ΠΎΡ‚ΠΎΠ±Ρ€Π°Π·ΠΈΡ‚ΡŒ Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π΅.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 6 notes

up
54
someone at attbi dot com ΒΆ
23 years ago
the idea is never to give out file names, line numbers, and cryptic codes to the user. Use trigger_error() after you used set_error_handler() to register your own callback function which either logs or emails the error codes to you, and echo a simple friendly message to the user.

And turn on a more verbose error handler function when you need to debug your scripts. In my init.php scripts I always have:

if (_DEBUG_) {
    set_error_handler ('debug_error_handler');
}
else {
    set_error_handler ('nice_error_handler');
}
up
17
Howard Yeend ΒΆ
16 years ago
trigger_error always reports the line and file that trigger_error was called on. Which isn't very useful.

eg:

main.php:
<?php
include('functions.php');
$x = 'test';
doFunction($x);
?>

functions.php:
<?php
function doFunction($var) {
if(is_numeric($var)) {
 /* do some stuff*/
} else {
 trigger_error('var must be numeric');
}
}
?>

will output "Notice: var must be numeric in functions.php on line 6"
whereas "Notice: var must be numeric in main.php on line 4" would be more useful

here's a function to do that:

<?php

function error($message, $level=E_USER_NOTICE) {
$caller = next(debug_backtrace());
trigger_error($message.' in <strong>'.$caller['function'].'</strong> called from <strong>'.$caller['file'].'</strong> on line <strong>'.$caller['line'].'</strong>'."\n<br />error handler", $level);
}
?>

So now in our example:

main.php:
<?php
include('functions.php');
$x = 'test';
doFunction($x);
?>

functions.php:
<?php
function doFunction($var) {
    if(is_numeric($var)) {
         /* do some stuff*/
    } else {
         error('var must be numeric');
    }
}

function error($message, $level=E_USER_NOTICE) {
    $caller = next(debug_backtrace());
    trigger_error($message.' in <strong>'.$caller['function'].'</strong> called from <strong>'.$caller['file'].'</strong> on line <strong>'.$caller['line'].'</strong>'."\n<br />error handler", $level);
}
?>

now outputs:

"Notice: var must be numeric in doFunction called from main.php on line 4"
up
7
richard at 2006 dot atterer dot net ΒΆ
20 years ago
Beware, trigger_error() is absolutely useless for transporting your own function's error messages in $php_errormsg:

ini_set('track_errors', TRUE);
function x() { trigger_error('MY ERROR'); }
@x();
echo "Error 1: \\"$php_errormsg\\"\\n";
@file_get_contents('/nonexisting');
echo "Error 2: \\"$php_errormsg\\"\\n";

This outputs:

Error 1: ""
Error 2: "failed to open stream: No such file or directory"

This behaviour is consistent with the description of $php_errormsg, which says that the variable will only be available within the scope in which the error occurred. The problem can be worked around with a custom error handler like the one below. However, I'm undecided whether changing the language in this way is good:

function errHandler($errno, $errstr, $errfile, $errline) {
  global $php_errormsg; $php_errormsg = $errstr;
}
set_error_handler('errHandler');
up
5
aydin dot kn12 at gmail dot com ΒΆ
11 years ago
If error_type is E_USER_ERROR then trigger_error throw FATAL ERROR and script stopped after this line.

<?php

$msg = 'This is the test message for echo';

trigger_error('Error message', E_USER_ERROR); // Script stopped after this line...

echo $msg; // This line does not appear...

?>
up
3
PhpMyCoder ΒΆ
15 years ago
For those of you looking to use your own file or line number in the error (possibly using debug_backtrace()) instead of the ones created by trigger_error(), here is a solution:
Create a custom function to handle E_USER_ERRORs that simply outputs the error type and message, while excluding the line number and file trigger_error() reports. You may also configure it to handle user warnings and notices if necessary (I did in the example below).

<?php
function error_handler($level, $message, $file, $line, $context) {
    //Handle user errors, warnings, and notices ourself
    if($level === E_USER_ERROR || $level === E_USER_WARNING || $level === E_USER_NOTICE) {
        echo '<strong>Error:</strong> '.$message;
        return(true); //And prevent the PHP error handler from continuing
    }
    return(false); //Otherwise, use PHP's error handler
}

function trigger_my_error($message, $level) {
    //Get the caller of the calling function and details about it
    $callee = next(debug_backtrace());
    //Trigger appropriate error
    trigger_error($message.' in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong>', $level);
}

//Use our custom handler
set_error_handler('error_handler');

//-------------------------------
//Demo usage:
//-------------------------------
function abc($str) {
    if(!is_string($str)) {
        trigger_my_error('abc() expects parameter 1 to be a string', E_USER_ERROR);
    }
}

abc('Hello world!'); //Works
abc(18); //Error: abc() expects parameter 1 to be a string in [FILE].php on line 31
?>

This is a pretty simple concept and I'm sure most of you know this, but for those that don't, let it serve as a good example!
up
-2
theking2 at king dot ma ΒΆ
1 year ago
The function trigger_error will terminate the script if $error_level is equal or higher than E_USER_ERROR. 

If you write your own error handler you will have to do these yourself.

Example in which we assume the global LOG constant points to a PSR2 logging interface.
<?php

set_error_handler( function ($errno, $errstr, $errfile, $errline) {
    // error was suppressed with the @-operator
    if( 0 === error_reporting() ) {
        return false;
    }
    switch($errno) {
        default:
            LOG->error( "Unknown error type: [$errno] $errstr", [ 'file' => $errfile, '@' => $errline ] );
            exit(1);

        case E_USER_ERROR: // fall through
        case E_WARNING: // treat PHP warnings are errors
            LOG->error( $errstr, [ 'file' => $errfile, '@' => $errline ] );
            exit(1);

        case E_USER_DEPRECATED:
        case E_DEPRECATED:
            LOG->error( "DEPRECATED $errstr", [ 'file' => $errfile, '@' => $errline ] );
            break;

        case E_USER_WARNING: // fall through
        case E_NOTICE: // treat PHP notices are warnings
            LOG->warning( $errstr, [ 'file' => $errfile, '@' => $errline ] );
            break;

        case E_USER_NOTICE:
            LOG->notice( $errstr, [ 'file' => $errfile, '@' => $errline ] );
            break;

        case E_ERROR: // fall through
        case E_RECOVERABLE_ERROR:
            LOG->critical( $errstr, [ 'file' => $errfile, '@' => $errline ] );
            exit(1);

    }
    /* Don't execute PHP internal error handler */
    return true;
} );
?>