Lara Ch
Lara Ch

Reputation: 165

Difference between error reporting and ERRMODE_EXCEPTION

I really can't understand why we should set at the top of a PHP data Object PDO connection file:

error_reporting(E_ALL);//When we are in a development process;

Or

error_reporting(0);//To shut off all errors when web app is live

And at the same time when we are creating our connection parameters we should add:

setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

My question is what is the difference and why we use the both of error_reporting and ERRMODE_EXCEPTION in the same file ? What are the roles of each one of them ? Is their any difference ?

Upvotes: 1

Views: 138

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157880

First of all, the initial statement is wrong.

error_reporting(E_ALL);

should be always the same in ALL environments.

While regarding the difference between the two,error_reporting() is a PHP-wide related setting, responsible for all PHP errors, and

setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

is for PDO only.

So you can tell why you have to use both: error_reporting(E_ALL) will make you aware of all PHP errors, like missing variable or a filesystem permission problem. While PDO's setAttribute just tells PDO to report its own errors and so make them available through error_reporting(E_ALL).

The proper settings would be:

error_reporting(E_ALL);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  • in ALL environments.

And then

ini_set('display_errors', 1);

when we are in a development process;

ini_set('display_errors', 0);
ini_set('log_errors', 1);

To shut off displaying all errors when web app is live, while logging them for the developer's reference.

Upvotes: 2

Related Questions