PHP: Stop Notice and Variable Warnings in Error Log Files

I‘m using PHP error logging in place of error displaying on production web sites. However, I do not want to see error messages about the use of uninitialized variables. I’d like to see all critical errors, except for notices and coding standards warnings. How do I disable error messages?

PHP provides various levels of error reporting using a bit-field as follows:

ADVERTISEMENTS


Error BitPurpose
E_ALL All errors and warnings (doesn’t include E_STRICT)
E_ERROR Fatal run-time errors
E_WARNING Run-time warnings (non-fatal errors)
E_PARSE Compile-time parse errors
E_NOTICE Run-time notices (these are warnings which often result from a bug in your code, but it’s possible that it was intentional (e.g., using an uninitialized variable and relying on the fact it’s automatically initialized to an empty string)
E_STRICTRun-time notices, enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
E_CORE_ERRORFatal errors that occur during PHP’s initial startup
E_CORE_WARNING Warnings (non-fatal errors) that occur during PHP’s initial startup
E_COMPILE_ERROR Fatal compile-time errors
E_COMPILE_WARNING Compile-time warnings (non-fatal errors)
E_USER_ERROR User-generated error message
E_USER_WARNING User-generated warning message
E_USER_NOTICE User-generated notice message

Show only errors

Open /etc/php.ini file
# vi /etc/php.ini

Set error_reporting as follows:

error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR

Alternately, you can show all errors, except for notices and coding standards warnings

error_reporting = E_ALL & ~E_NOTICE

Save and close the file. Restart apache web server:
# /etc/init.d/httpd restart

If you are using nginx/lighttpd, try:
# service nginx reload
# service lighttpd restart

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
13 comments… add one
  • Steve Dec 21, 2008 @ 11:56

    I am running my web site on a shared host.
    In my case the /etc/php.ini is not accessible, but they allow me to use local php.ini in each folder.
    Other web hosting companies allow to modify .htaccess, so the alternative solution for this:

    error_reporting = E_ALL & ~E_NOTICE

    when using .htaccess is:

    php_value error_reporting 2039

  • Samir Dec 21, 2008 @ 16:28

    In the case where you have a shared host, the php.ini file needs to be recursively copied to each folder that will be executing PHP code.
    To automate this process, you can use this script:

    COMPLETE!

  • Hossam el din Dec 22, 2008 @ 8:58

    You could instead override the values of PHP configuration file “php.ini” inside your code by using the ini_set function , which has two parameters , one of them is the name of the configuration value ” for example ‘display_error’ ” and the other is the value we want to set it to ‘ 0 or 1 as an example for display_error’.

    in this case you don’t even need to restart the httpd , and you don’t affect other websites on a shared host

  • Mani May 20, 2010 @ 9:39

    How to disable notice and warning error without using in PHP.ini file Like(error_reporting = E_ALL & ~E_NOTICE)

    Please let me know if you know any other alternate solutions.

    Thanks
    Mani

  • Nikesh Oct 15, 2010 @ 19:37

    very very useful site for me. I am getting lots of linux tips from here. Thank you very much and keep it up in the future too.

  • BarryW Jun 24, 2011 @ 16:02

    Starting with PHP 5.3 there is now also E_DEPRECATED

  • dev Jun 5, 2012 @ 14:46

    Same thing like Mani, How can I get out of these issues without accessing php.ini
    Thanks in advance

  • Mahender Sep 1, 2012 @ 4:25

    very very useful site for me. I am getting lots of linux tips from here. Thank you very much and keep it up in the future too.

  • Brett Dec 9, 2013 @ 17:49

    This article isn’t listed under the ‘PHP’ tag/category, just so you know!

    • 🐧 Nix Craft Dec 9, 2013 @ 19:42

      Just added to php category! I appreciate your post.

  • Peter Raig Feb 22, 2014 @ 16:39

    Bingo! right on the money advice!

  • Mukopaje Sep 9, 2014 @ 23:02

    copy and paste the below code just after your php opening tag in your code…

    error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
    ini_set(“display_errors”,”off”);

    • AJIT Nov 3, 2014 @ 6:13

      Thanks brother.Mukopaje

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.