title: Best practice for error handling using PDO
body: <p><strong>Problem:</strong></p>
  
  <p>Finding best practice for error handling using PDO. The options I have found on websites, SO, books, etc.</p>
  
  <ol>
  <li>A great number of websites say you should echo your error messages in your <code>catch</code> block. </li>
  <li>A large number of users on SO say that you should never echo error messages due to security risks. </li>
  <li>Others are recommending logging it to a log file outside the document root. </li>
  <li>Some use error handling to log it to a SQL table. </li>
  </ol>
  
  <p>With a multitude of options, it gets quite easy to drown into what option you should be using. Of course you could use a MVC framework and let it handle error logging for you, but how would it look like if you are not using MVC.</p>
  
  <p>As I have understood it error handling should like the following in <strong>development environment</strong>:</p>
  
  <pre><code>display_errors = On
  display_startup_errors = On
  error_reporting = -1
  log_errors = On
  </code></pre>
  
  <p>Or if no access is available to the php.ini file:</p>
  
  <pre><code>error_reporting(-1);
  ini_set("display_errors", 1);
  </code></pre>
  
  <p>And in <strong>production environment</strong>:</p>
  
  <pre><code>display_errors = Off
  display_startup_errors = Off
  error_reporting = E_ALL
  log_errors = On
  </code></pre>
  
  <p>Or if no access is available to the php.ini file:</p>
  
  <pre><code>error_reporting(0);
  </code></pre>
  
  <p>To take an example of a database connection in <strong>production environment</strong>.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>&lt;?php
    // Error handling
    error_reporting(0);
  
    // Get credentials from outside document root
    require_once('../settings.php');
  
    // Tests connection to database
    try {
      $dbh = new PDO(
              sprintf(
                'mysql:host=%s;dbname=%s;port=%s;charset=%s',
                $settings['host'],
                $settings['name'],
                $settings['port'],
                $settings['charset']
              ),
              $settings['username'],
              $settings['password']
      );
      // Prevents emulated prepares and activates error handling
      // PDO::ERRMODE_EXCEPTION
      $dbh-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
      $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    // Catches errors raised by PDO
    catch (PDOException $e) {
      // Prints error messages to file
      file_put_contents('/home/ubuntu/errors.log', 'Error: ' . $e-&gt;getMessage() . PHP_EOL, FILE_APPEND);
      // Shows generic error message to user
      header('Location: 404.php');
      exit;
    }
  ?&gt;
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <ul>
  <li>What is the best practice for handling errors in general in PHP?</li>
  <li>What is the best practice for handling errors in the catch-block?</li>
  </ul>