PHP E_USER_WARNING

We may use the E_USER_WARNING error type when we wish to trigger a recoverable warning error, and allow the script to continue executing.

896 views
d

By. Jacob

Edited: 2020-02-25 02:41

E_USER_WARNING in PHP.

E_USER_WARNING can be used to trigger a recoverable, non-fetal, warning error using the trigger_error function. This basically means that the script should be allowed to continue executing.

We can trigger a E_USER_WARNING in order to inform a developer that a insignificant problem occurred, that probably should not occur.

A good example of when a warning is delivered by PHP is when we are trying to include a file that does not exist via include, if we instead use the require keyword, a fetal error will be triggered and the script should not continue.

It is usually best to account for all scenarios in the code, which means we should first check if a file exists before attempting to include it, or we can simply use require if we do not care about the cause of the failure.

See also: Include, Require, and Include once

To trigger a E_USER_WARNING error we can do like below:

trigger_error("A optional file did not exist, but since the file was optional, the script was allowed to continue.", E_USER_WARNING);

Tell us what you think:

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials