The user_error Function

The user_error function (also known as trigger_error) generates a custom error message. It accepts a message string as the first parameter and an error type (E_USER_NOTICE, E_USER_WARNING, or E_USER_ERROR) as the second parameter.

Syntax

phpuser_error(message, error_type);

Example

Let's generate a simple notice (E_USER_NOTICE):

php<?php
  user_error('This is a notice', E_USER_NOTICE);
?>

Code execution result:

phpNotice: This is a notice

Example

Let's generate a warning (E_USER_WARNING):

php<?php
  user_error('Warning: invalid value', E_USER_WARNING);
?>

Code execution result:

phpWarning: Warning: invalid value

Example

Let's generate a fatal error (E_USER_ERROR):

php<?php
  user_error('Critical error occurred', E_USER_ERROR);
  echo 'This line will not execute';
?>

Code execution result:

phpFatal error: Critical error occurred