PHP trigger_error() Function

The trigger_error() is an inbuilt function of PHP and is used to generates a user-level error/warning/notice message. 
It will returns FALSE if wrong error_type is specified, TRUE otherwise.


Syntax

trigger_error(error_msg,error_type)

Parameters 

error_msg: The designated error message for this error. It's limited to 1024 bytes in length.

error_type: The designated error type for this error. 

Possible error types:
E_USER_ERROR − Fatal user-generated run-time error. Errors that can not be recovered from. Execution of the script is halted.

E_USER_WARNING − Non-fatal user-generated run-time warning. Execution of the script is not halted.

E_USER_NOTICE − Default. User-generated run-time notice. The script found something that might be an error, but could also happen when running a script normally.


2
3
4
5
<?php
$my_num=15;
if ($my_num>10) {
    trigger_error("Number cannot be larger than 10");
}
?>


Output.

It will show the following custom error
Notice: Number cannot be larger than 10 in C:\xampp\htdocs\php_error.php on line 4

 trigger_error() function example
<?php
 function my_func($var) {
    if(is_numeric($var)) {
    /* do some stuff*/
    } else {
    trigger_error('variable must be numeric');
   }
}

$x = 'apple';
my_func($x);

?>

Output.

It will show the following custom error
Notice: variable must be numeric in C:\xampp\htdocs\roll-generator\php_error.php on line 6


No comments

Powered by Blogger.