PHP: Error management
- Ameni OUERTANI
- Nov 21, 2018
- 2 min read
A good script should theoretically not generate an error. Some errors are
unavoidable, such as those due to faulty connection problems or
bug on the server.

Here we are more interested in the errors that can be caused by actions of the user as erroneous inputs that provok stopping the script or those that may occur when attempting to access a non-existent resource.
The purpose of error management is to report the problems "to the visitor" and
avoid displaying raw error messages as PHP sends them to the browser.
Deleting error messages
If you write the code below:
<? Php
$ A = 10;
$ B = 0;
echo $ a / $ b; ←
fopen ( "inconnu.txt", "r"); ←
?>
the following messages appear:
Warning: Division by 0 in c: \ wamp5 \ www \ php5 \ c3instructions \ instruct3.15a.php
on line 4
Warning: fopen (unknown.txt) [function.fopen]: failed to open stream
directory in c: \ wamp5 \ www \ php5 \ c3instructions \ instruct3.15a.php on line 5
The first message corresponds to the division by 0, and the second to the
attempt to open a file that does not exist. These messages displayed
in the course of an HTML page have the worst effect for visitors to the site.
To avoid displaying PHP error messages in the browser, there are
following elementary means:
Precede the call with a function of the @ character by writing for example @fopen ( "inconnu.txt", "r").
Use the function error_reporting(), which allows to display only certain messages depending on the type of error. Its syntax is int error_reporting ([int level]). It's the level parameter that allows to choose the level of display of error messages.
Its main values are shown in Table next.
You can combine multiple values with the | operator, as in the following code:
error_reporting (E_WARNING | E_PARSE);
which only displays alerts and syntax errors.
To block all error messages, it is necessary to pass to the function the parameter 0. This function must be used from the beginning of the script.

It is obvious that during the script development phase, these methods must
be disabled to allow the programmer to be alerted about the causes and the
location of errors.
Comments