Menu Home

trigger_error

osbo.com | PHP | Functions | trigger_error

Generates a user-level error/warning/notice message

Syntax

trigger_error ( string $error_msg [, int $error_type = E_USER_NOTICE ] ) : bool

Parameters

error_msg

The designated error message for this error. It's limited to 1024 bytes in length. Any additional characters beyond 1024 bytes will be truncated.

error_type

The designated error type for this error. It only works with the E_USER family of constants, and will default to E_USER_NOTICE.

Return

This function returns FALSE if wrong error_type is specified, TRUE otherwise.

Examples

<?

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    switch ($errno) {
        case E_USER_ERROR:
            echo "ERROR [$errno] $errstr<br>\n";
            exit(1);
            break;
    }
    return true;
}

function divide($numerator, $denominator) {
    if ($denominator == 0) {
        trigger_error("Cannot divide by zero", E_USER_ERROR);
    }
    return $numerator / $denominator;
}

set_error_handler("myErrorHandler");

echo divide(10, 0);

?>
ERROR [256] Cannot divide by zero<br>

PHP