How to Display PHP Errors and Warnings?

Are your PHP scripts not working, but you’re seeing a blank page? Don’t worry — this is common when PHP errors and warnings are turned off.

Learning how to display PHP errors is essential for debugging and fixing problems quickly. In this beginner-friendly tutorial, you’ll learn how to enable PHP error reporting, show PHP warnings, and set up your server to make debugging easier.

Whether you’re using XAMPP, MAMP, or a live server, this guide will help you turn on full PHP error visibility step by step.

Why Display PHP Errors?

Showing errors helps you:

  • Spot bugs and fix them fast
  • Understand what’s going wrong
  • Improve code quality
  • Save time during development

1. Show Errors with PHP Code

Add these lines at the top of your PHP file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

This will make PHP show all errors and warnings on the screen.

2. Show Errors Using php.ini (Server Settings)

If you want errors to show on all files:

  1. Open your php.ini file
  2. Find and change:
display_errors = On
display_startup_errors = On
error_reporting = E_ALL
  1. Restart your server (Apache, Nginx, etc.)

3. Log Errors Instead (Safe for Live Sites)

For live (production) websites, don’t show errors to users. Instead, log them like this:

display_errors = Off
log_errors = On
error_log = /path/to/php-error.log

This keeps your site safe while you still get the error info.

4. Test Example

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

// Intentional error
echo $undefined_var;

This will show:

Warning: Undefined variable…

Conclusion

Showing PHP errors helps you find bugs fast. Use ini_set() during development, and switch to logging on live sites to stay secure.

Leave a Reply

Your email address will not be published. Required fields are marked *