Can we hide errors in php?

This article describes how to enable and disable PHP error messages by using the display_errors directive in an .htaccess file.

The information in this article only applies to certain types of hosting accounts. To determine whether or not the information below applies to your account, please see this article.

This article assumes that you have already set up a custom .htaccess file. If you have not already set up a custom .htaccess file, please read this article first.

Error messages and the display_errors directive

By default, PHP displays error messages in a user's web browser. This feature is very useful when you are initially developing and debugging your website. However, when website development is complete, displaying error messages can be a security risk. Error messages can reveal information about your website, such as path information and variables, that should be kept private.

The display_errors directive controls whether or not PHP displays error messages in users' web browsers. To set the display_errors directive, follow these steps:

  1. Log in to your account using SSH.
  2. Use a text editor to modify the .htaccess file as follows:
    • To prevent PHP from displaying error messages, add the following line:
      php_flag display_errors Off
    • To allow PHP to display error messages, add the following line:

      php_flag display_errors On
  3. Save the changes to the .htaccess file and exit the text editor.
  4. To verify that the new setting is active, create a PHP test file that contains the following code in the same directory where the .htaccess file is located:
    <?php phpinfo(); ?>
  5. Load the test file in your web browser, and then search for the name of the directive. The Local Value column should display the new setting that you specified in the .htaccess file.

More Information

  • To view a complete list of PHP directives, please visit http://www.php.net/manual/en/ini.list.php.
  • For more information about the display_errors directive, please visit http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors.

PHP Error Reference

Example

Specify different error level reporting:

<?php
// Turn off error reporting
error_reporting(0);

// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Report all errors
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set("error_reporting", E_ALL);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
?>



Definition and Usage

The error_reporting() function specifies which errors are reported.

PHP has many levels of errors, and using this function sets that level for the current script.


Syntax

Parameter Values

ParameterDescription
level Optional. Specifies the error-report level for the current script. Error numbers and named constants are accepted. Note: Named constants are recommended to ensure compatibility for future PHP versions


Technical Details

Return Value:Returns the old error reporting level or the current error reporting level if no level parameter is given
PHP Version:4.0+
PHP Changelog:PHP 5.4: E_STRICT is now a part of E_ALL.
PHP 5.3: New: E_DEPRECATED and E_USER_DEPRECATED.
PHP 5.2: New: E_RECOVERABLE_ERROR.
PHP 5.0: New: E_STRICT.

PHP Error Reference


In rare cases, a Plugin or Theme will cause “errors” on the front-end of websites. Some hosts such as WP Engine technically “hide” errors from being displayed in the actual browser, but the errors themselves may still be happening. Other hosts, such as ServerPilot do not prevent these errors from displaying.

So in addition to the errors being “logged” into an error log file, they may also display at the top of your website’s content. Obviously not ideal on a production site.

If you have errors showing that aren’t causing actual PROBLEMS, then it should be OK to hide them for the time being. However please reach out to the plugin developer with the error lines, so they can know about fixing it. Most of the errors are related to a new “array” warning that wasn’t a big deal in PHP 5.6, but is now technically wrong in PHP 7. Though, the warning rarely actually breaks anything.

To Remove the Errors from the Front-end

Please take a look at this: aristath.github.io/blog/wp-hide-php-errors

The bottom chunk of code is what you’d need. Here it is:

ini_set('display_errors','Off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_DISPLAY', false);
  1. FTP into your server.
  2. Find your app’s wp-config.php file.
  3. Open that file and add those lines of code before the
    /* That’s all, stop editing! Happy blogging. */ line
  4. For example: https://d.pr/free/i/glwovT
  5. Save the file and overwrite the old one for your app
  6. The errors should now be suppressed, to no longer display

Jon Fullerhttps://freshysites.com/team/jon-fuller/

Jon is from the Binghamton, NY area and graduated from the School of Art and Design at Alfred University with a Bachelor of Fine Arts. Concentrated studies were in Graphic Design and Video. Jon has an appreciation of all aspects of art and design, and has an extensive background in both print media and web — with a thorough understanding of video and photography. Jon came to FreshySites with 9 years of graphic design and web experience. He continues to make a habit of researching new tools and technologies to broaden his knowledge in the web development industry. He’s a tech junkie who has a passion for implementing new techniques and improving his surroundings. Outside of work, Jon also enjoys going on new adventures with his wife, exercising, and sipping on quality craft beer.

See our featured website design work

Check out some of the beautiful websites we’ve built for over 2,000 clients.

We offer WordPress support & maintenance

Shake the stress of ongoing maintenance with plans supported by our team of WordPress experts.

How do I stop PHP from showing errors?

Use a text editor to modify the .htaccess file as follows:.
To prevent PHP from displaying error messages, add the following line: php_flag display_errors Off..
To allow PHP to display error messages, add the following line: php_flag display_errors On..

How do I hide warnings and notices in PHP?

In the current file, search for the line of code error_reporting. There will be a line of Default Value: E_ALL as shown below: Replace this line of code with Default Value: E_ALL & ~E_NOTICE. It will display all the errors except for the notices.

How do I hide PHP errors in WordPress?

Turning off PHP Errors in WordPress ini_set ( 'error_reporting' , E_ALL ); define( 'WP_DEBUG' , false); define( 'WP_DEBUG_DISPLAY' , false); Don't forget to save your changes and upload your wp-config.

How do I fix PHP errors?

Editing the php..
Log into your cPanel..
Go to the File Manager. ... .
Find the “Error handling and logging” section in the php.ini. ... .
Next you can set the display_errors variable to On or Off to either show the errors on your website or not..