Introduction: Why Does PHP Stay Silent When Something Goes Wrong?
\n\nHave you ever written a PHP script, run it in your browser — and seen... a blank white page? No messages, no hints. Just silence and a feeling that you did something wrong. Sound familiar?
\n\nThis is one of the most common problems beginners face in PHP. The question “How can I get useful error messages in PHP?” on Stack Overflow has gathered thousands of views. And it's no wonder: without clear error messages, debugging turns into guesswork.
\n\nIn this article, we'll break down how to make PHP talk, so it honestly tells you what exactly went wrong. You'll learn which settings control error output, how to enable them at different stages of development, and what to do if errors still don't show up. Let's go!
\n\nWhy Does PHP Hide Errors by Default?
\n\nBefore we start tinkering with settings, let's understand the reason. PHP is a language that runs on the server. On production servers, showing errors to users is bad practice. Imagine: instead of a beautiful online store page, the client sees something like Fatal error: Call to undefined function. This is not only ugly but also unsafe — an attacker could learn the structure of your code.
Therefore, PHP developers made it so that by default, error output to the browser is disabled. But on a local server (e.g., OpenServer, XAMPP, MAMP) or during development, this is very inconvenient. Let's fix that.
\n\nMethod 1: Enable Errors via php.ini (Globally)
\n\nThe most reliable way is to change the configuration file php.ini. Find it in your PHP folder (or in the control panel of your local server). Open the file and find (or add) the following lines:
display_errors = On\nerror_reporting = E_ALL\n\n\nWhat does this mean?
\n- \n
display_errors = On— allows errors to be displayed directly on the screen (in the browser or console). \n error_reporting = E_ALL— tells PHP to show all types of errors: from warnings to fatal errors. \n
After making changes, restart your web server (Apache, Nginx). Now any script will output errors.
\n\nImportant: Never leave display_errors = On on a production server. Use Off and configure logging (more on that below).
Method 2: Enable Errors Directly in the Script (Quick Way)
\n\nIf you don't have access to php.ini (e.g., you're on shared hosting), or you want to enable errors only for one file, use the ini_set() and error_reporting() functions. Add these lines at the very beginning of your PHP script:
<?php\nini_set('display_errors', 1);\nini_set('display_startup_errors', 1);\nerror_reporting(E_ALL);\n\n// Your code below\n$foo = 1/0; // Will cause Warning: Division by zero\n?>\n\n\nLet's break it down line by line:
\n- \n
ini_set('display_errors', 1)— enables error output to the screen. \n ini_set('display_startup_errors', 1)— enables errors that occur during PHP startup (e.g., if the script itself cannot load). \n error_reporting(E_ALL)— shows all error levels. \n
This method works instantly and does not require a server restart. Ideal for quick debugging.
\n\nMethod 3: Use .htaccess (for Apache)
\n\nIf you have an Apache server, you can enable errors via the .htaccess file in the site's root folder. Add this there:
php_flag display_errors on\nphp_value error_reporting E_ALL\n\n\nAfter saving the file, errors will start showing for all scripts in this folder and its subfolders. Convenient when you need to quickly enable debugging