Question
I often run a PHP script and get only a blank page with no error message. The problem could be a simple syntax error, such as a missing semicolon or unmatched bracket, a failed function call, or another runtime issue.
This makes debugging difficult, and I end up commenting out code or adding many echo statements just to find the cause.
Is there a better way to make PHP show useful error messages, similar to the detailed errors you might see in Java?
Short Answer
By the end of this page, you will understand how PHP error reporting works, how to display errors during development, how to log them safely, and why a blank page usually means an error is being hidden rather than absent.
Concept
PHP can report many kinds of problems: syntax errors, warnings, notices, deprecated features, and fatal errors. If error display is turned off, a script may fail and show only a blank page, which is often called the white screen of death.
The key idea is that PHP has two separate error behaviors:
- Display errors: show errors directly in the browser or terminal
- Log errors: write errors to a file or server log
During development, you usually want to display and log errors. In production, you usually want to log errors but not display them, so users do not see internal details.
The most common tools are:
error_reporting(E_ALL);
ini_set('display_errors', '1');
These tell PHP to report all errors and display them.
Why this matters:
- It helps you find syntax mistakes quickly
- It reveals failed function calls and undefined variables
- It makes debugging much faster than adding many
echostatements - It supports safer production setups through logging instead of screen output
A blank page usually does not mean nothing went wrong. It often means PHP encountered an error, but your configuration hid it.
Mental Model
Think of PHP errors like warning lights in a car.
- If the dashboard lights are enabled, you immediately see that something is wrong.
- If the lights are disabled, the car may still fail, but you get no clue why.
error_reporting() decides which warning lights are active.
display_errors decides whether those warning lights are shown to you on screen.
log_errors decides whether the issue is written into a maintenance record for later review.
So when you get a blank page, the engine may already be telling you something is wrong—you just are not seeing the dashboard.
Syntax and Examples
To show useful errors in PHP during development, use this near the top of your script:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
What each line does
error_reporting(E_ALL);enables reporting for all PHP errors, warnings, and noticesini_set('display_errors', '1');tells PHP to show errors in the outputini_set('display_startup_errors', '1');shows errors that happen during PHP startup
Example: undefined variable
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$total = $price + 10;
echo $total;
You would see an error because $price was never defined.
Example: syntax error
Step by Step Execution
Consider this script:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$name = "Sam";
echo $nama;
Step by step
-
PHP reads
error_reporting(E_ALL);- All error levels are enabled.
-
PHP reads
ini_set('display_errors', '1');- Errors will be shown in the browser.
-
PHP assigns
"Sam"to$name.- This works normally.
-
PHP reaches
echo $nama;$namadoes not exist.- PHP raises a warning about an undefined variable.
-
Because error display is enabled, the warning appears on screen.
- Without error display, you might only see missing output or a blank page.
Another example with a fatal error
Real World Use Cases
Useful PHP error messages are important in many practical situations:
- Building forms: find undefined
$_POSTkeys, validation mistakes, or type problems - Working with databases: detect failed connections, bad SQL calls, or null values
- Calling APIs: catch invalid responses, missing fields, or timeout-related warnings
- File uploads: find permission errors, missing directories, or failed file moves
- Template rendering: spot missing variables in included PHP views
- CLI scripts: understand why scheduled jobs or maintenance scripts failed
Example in a form handler:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$email = $_POST['email'];
echo "Email: " . $email;
If the form did not submit email, PHP can warn you immediately, which helps you fix the logic faster.
Real Codebase Usage
In real projects, developers usually combine error display, logging, validation, and structured debugging.
Common patterns
1. Development vs production configuration
Developers often use different settings depending on environment:
<?php
if ($_ENV['APP_ENV'] === 'development') {
error_reporting(E_ALL);
ini_set('display_errors', '1');
} else {
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
}
2. Logging instead of showing errors to users
In production, showing raw errors can expose file paths, SQL details, and internal code structure. Logging is safer.
3. Guard clauses and validation
Developers reduce errors by checking inputs early:
<?php
if (!isset($_POST['email'])) {
exit('Email is required');
}
This avoids later warnings and makes the failure clearer.
Common Mistakes
Here are common beginner mistakes when trying to get useful PHP errors.
1. Enabling errors too late
Broken example:
<?php
echo "Start"
error_reporting(E_ALL);
ini_set('display_errors', '1');
Why it fails:
- The syntax error happens before PHP can run the error settings.
How to avoid it:
- Enable error display in
php.ini,.htaccess, virtual host config, or server config when needed.
2. Using display errors in production
Broken approach:
ini_set('display_errors', '1');
Why it is risky:
- Users may see sensitive paths, SQL messages, or application internals.
How to avoid it:
- Use
display_errors=0andlog_errors=1in production.
3. Assuming all problems are exceptions
In PHP, many issues are warnings or notices, not exceptions. If you only use , some errors still will not be handled the way you expect.
Comparisons
| Setting or Tool | What it does | Best use | Risk |
|---|---|---|---|
error_reporting(E_ALL) | Chooses which errors PHP reports | Development and production | Low |
display_errors=1 | Shows errors on screen | Development only | High in production |
log_errors=1 | Writes errors to logs | Development and production | Low |
ini_set() | Changes settings at runtime | Quick local debugging | May be too late for parse/startup errors |
php.ini | Global PHP configuration |
Cheat Sheet
// Development settings
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
// Log errors
ini_set('log_errors', '1');
ini_set('error_log', __DIR__ . '/php-errors.log');
Key rules
- Use
E_ALLto report everything useful - Use
display_errors=1in development - Use
display_errors=0in production - Use
log_errors=1in production - Parse errors may require
php.inior server-level config - Avoid the
@operator when debugging
Useful places to check
- PHP script with
ini_set() php.ini- Apache or Nginx logs
- PHP-FPM logs
- Hosting dashboard error logs
FAQ
Why does PHP show a blank page instead of an error?
Usually because an error occurred but display_errors is off, so the message is hidden.
What is the fastest way to show PHP errors during development?
Add the following near the top of your script:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Why do syntax errors sometimes still not show?
Because the script may fail before those lines execute. In that case, enable errors in php.ini or server configuration.
Should I enable display_errors on a live website?
No. It can expose sensitive internal details. Use logging instead.
Where can I find PHP errors if they are not shown in the browser?
Check PHP error logs, Apache or Nginx logs, PHP-FPM logs, or your hosting control panel logs.
What does E_ALL mean in PHP?
It tells PHP to report all available error levels, including warnings and notices.
Does try/catch replace PHP error reporting?
No. try/catch handles exceptions, but many PHP issues are warnings, notices, or fatal errors outside normal exception flow.
Mini Project
Description
Create a small PHP debugging helper script that demonstrates how error reporting, display settings, and logging work. This project is useful because it mirrors a common real-world situation: a script fails, and you need a reliable way to see what went wrong instead of getting a blank page.
Goal
Build a PHP script that enables full error reporting, triggers a few common errors, and logs them to a file for inspection.
Requirements
- Enable all PHP error reporting in the script.
- Display errors in the browser for development.
- Log errors to a local file.
- Include at least one undefined variable warning.
- Include at least one failed function or file operation.
- Print a normal success message so you can compare working output with error output.
Keep learning
Related questions
Are PDO Prepared Statements Enough to Prevent SQL Injection in PHP?
Learn how PDO prepared statements prevent SQL injection in PHP, what they protect, and the mistakes that still leave MySQL apps vulnerable.
Can You Bind an Array to an IN Clause in PHP PDO?
Learn how PDO handles placeholders in IN() clauses, why arrays cannot be bound directly, and the safe PHP pattern to build dynamic queries.
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.