PHP E_WARNING

Warnings are errors that occur when something is probably not expected or desired, and generally developers try account for them in their code to avoid serious issues.

1432 views
d

By. Jacob

Edited: 2021-04-18 08:47

E_WARNING in PHP

An E_WARNING error is not always serious, and often you can safely ignore it. However, it is still best if you try to avoid warnings before they happen.

A warning can be triggered when including a file that does not exist, and when sending HTTP headers after sending HTTP body output. There are also other causes for this error, and most often, it simply happens because there is something the developer overlooked.

To only show E_WARNING errors, you may use the error_reporting function:

error_reporting(E_WARNING);

To instead show all errors, use E_ALL:

error_reporting(E_ALL);

Custom error messages

To trigger a custom warning error, the E_USER_WARNING type should be used:

trigger_error("This situation was not expected, but assuming the developer knows about it, we continue!", E_USER_WARNING);

Note. Only E_USER* types should be used with trigger_error.

Causes for E_WARNING

Trying to send headers after they have been sent earlier in the code will result in:

PHP Warning: Cannot modify header information - headers already sent in...

Another common cause is including trying to include a file that does not exist:

[php7:warn] [pid 8544] [client 127.0.0.1:57390] PHP Warning: include(test.php): failed to open stream: No such file or directory in /var/www/test/test.php on line 3

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