Show and log PHP errors while developing

Beginner  Nov 3, 2026

See every problem on your computer, and hide them from visitors on the live site.

Show and log PHP errors while developing
The steps of this tutorial at a glance.
The steps of this tutorial at a glance.

On your computer

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
How the code in this section fits into the page.
How the code in this section fits into the page.

On the live site

Never show errors to visitors. They reveal file paths and sometimes passwords. Log them instead:

ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', __DIR__ . '/logs/php-error.log');

Keep the logs folder out of public access.

Quick debugging tools

var_dump($value);                 // type and value
echo '<pre>', print_r($arr, true), '</pre>';
error_log('Reached step 3: ' . json_encode($data));

Read the error message

A message has three useful parts: what went wrong, the file, and the line number. Start at that line, and also look at the line above it (a missing ; is reported on the next line).

Common beginner errors

  • Undefined variable or Undefined index: you used something before setting it. Use ?? for a default: $_GET['id'] ?? 0.
  • headers already sent: something was printed before header() or session_start(). Look for spaces before <?php.
  • Call to undefined function: a typo, or a missing extension.
The main points of this section.
The main points of this section.

More tutorials