PHP error handling function trigger_error()


PHP function trigger_error() generates a user-level error or warning or notice.

The function can be used either along with the built-in error handler or with a user defined function which is set as the new error handler (set_error_handler()).

If you want to generate a specific response to an exception at runtime, using this function is useful.


PHP 4 >= 4.0.1, PHP 5.


trigger_error (error_msg,  error_type )


Parameters Description Required / Optional Type
error_msg A string which will be displayed as an error message. Required string
error_type Specifies the error type for an error message.
Values may be -
Optional integer

Return Values:

The function returns FALSE if the wrong error_type is specified. Otherwise, it returns TRUE.


  $a = 12;
  $b = 5;
  if($a%$b == 0)
  trigger_error("This is an error",E_USER_NOTICE );

Previous: set_exception_handler()
Next: user_error()

PHP: Tips of the Day

PHP: How to convert string to boolean?

Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP (taken from the documentation for empty):

  1. "" (an empty string);
  2. "0" (0 as a string)

If you need to set a boolean based on the text value of a string, then you'll need to check for the presence or otherwise of that value.

$test_mode_mail = $string === 'true'? true: false;

EDIT: the above code is intended for clarity of understanding. In actual use the following code may be more appropriate:

$test_mode_mail = ($string === 'true');

or maybe use of the filter_var function may cover more boolean values:

filter_var($string, FILTER_VALIDATE_BOOLEAN);

filter_var covers a whole range of values, including the truthy values "true", "1", "yes" and "on".

Ref : https://bit.ly/33SQ3eP