FAQ PHP & Development

How to turn PHP error display on while you debug

A short-term switch that shows errors on the page while you debug, and must be turned off again before anyone else sees the site.

Updated 4 min read Beginner

Two directives control whether PHP prints errors straight onto the page: display_errors and error_reporting. Turning them on shows you exactly what a script is failing on instead of a blank page or a generic message, which makes them the fastest way to see what is actually wrong — provided you switch them off again once you have your answer.

Turning it on

The safest way to do this for a single debugging session is at the top of the specific script, so the change is scoped and temporary rather than applied site-wide:

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);

Add those two lines above everything else in the file you are troubleshooting, reload the page, and PHP will print the error, its file and its line number directly where the failure happened.

If the page is already fatally erroring before your code runs

Adding lines to the script does not help if PHP is failing before it reaches them — a parse error, for instance, stops the whole file being interpreted at all. In that case, set the values through the PHP settings area of your control panel or a user-level php.ini instead, so the setting takes effect before the broken file is even parsed:

display_errors = On
error_reporting = E_ALL
Turn it back off before you finish

Displayed errors reveal server file paths, database details in some cases, and the internal structure of your code to anyone who happens to trigger the same error — including automated scanners looking for exactly that kind of information. Once you have found what you needed, set display_errors back to Off and rely on the error log instead, which records the same detail without putting it in front of visitors.

Why the error log is the better default

Leaving errors logged to a file rather than displayed on the page gives you the same information with none of the exposure, and it does not depend on you being the one who happens to trigger the failure — the log captures it whether you are watching or not. Reach for display_errors only for the length of an active debugging session, and treat the log as the tool you leave running the rest of the time.

Related reading