How to Turn Off Error Display in PHP: Simple Guide
To turn off error display in PHP, use
ini_set('display_errors', '0') and set error_reporting(0). This stops PHP from showing errors on the screen but still allows logging if configured.Syntax
Use the ini_set function to change the display_errors setting at runtime. Use error_reporting to control which errors are reported.
ini_set('display_errors', '0'): Turns off showing errors on the screen.error_reporting(0): Tells PHP not to report any errors.
php
<?php ini_set('display_errors', '0'); error_reporting(0); ?>
Example
This example shows how to turn off error display. The code tries to use an undefined variable, but no error message will appear.
php
<?php ini_set('display_errors', '0'); error_reporting(0); // This will cause a notice normally, but it won't show echo $undefined_variable; // Output a message to confirm script runs echo "Script ran without showing errors."; ?>
Output
Script ran without showing errors.
Common Pitfalls
Many beginners forget to disable error display in production, which can expose sensitive information. Also, setting display_errors in the wrong place or after errors occur won't hide them.
Remember to set these at the start of your script or in your php.ini file.
php
<?php // Wrong: setting display_errors after error occurs echo $undefined_var; // Notice shown here ini_set('display_errors', '0'); error_reporting(0); // Right: set before any code that may cause errors ini_set('display_errors', '0'); error_reporting(0); echo $undefined_var; // No notice shown ?>
Output
Notice: Undefined variable: undefined_var in ... (only in wrong example, no output in right example)
Quick Reference
| Setting | Purpose | Example |
|---|---|---|
| ini_set('display_errors', '0') | Turn off error display on screen | ini_set('display_errors', '0'); |
| error_reporting(0) | Disable reporting of all errors | error_reporting(0); |
| php.ini setting | Permanent change in config file | display_errors = Off |
Key Takeaways
Use ini_set('display_errors', '0') and error_reporting(0) at the start of your PHP script to hide errors.
Turning off error display helps keep error details hidden from users in production.
Set these configurations early to avoid showing errors before they are disabled.
For permanent changes, update the php.ini file with display_errors = Off.
Remember error logging can still be enabled separately to track issues without showing them.