0

EDIT: My Environment is the following (this is relevant to the answer):

  • Microsoft IIS 10.0 Express
  • Windows 7 Professional SP1
  • PHP Version 5.6.16 x86 FastCGI

I am trying to figure out what purpose the setting "display_errors" has. In the following code, I am attempting to hide the PHP Notice that is thrown at line 9. Naturally one would think that simply setting

 display_errors = 'Off'

would accomplish this, but it seems to have no effect.

I am of the understanding that "error_reporting" lets you choose which errors are caught, and "display_errors" determines whether or not the error messages are echoed to the user. Am I mistaken? In most of my Google searches, I see people setting both

error_reporting = 0

and

display_errors = 'Off'

But, why even bother with

   display_errors = 'Off'

when it does not have an effect anyway?

My test code:

<?php
//ini_set("error_reporting", 0);
ini_set("display_errors", "Off");
ini_set("display_startup_errors", "Off");
date_default_timezone_set("America/Chicago");

echo "Hey<br />";

echo $myarr["hey"];

When I run this code, I see the following error message (this is a link to the image, since I don't have enough reputation to post it here):

https://i.stack.imgur.com/qB6Au.png

PHP: The error message generated for line 9 should be hidden from the user. Why does "display_errors" exist if it does nothing?

user3163495
  • 2,425
  • 2
  • 26
  • 43
  • error_reporting is a function not a varible. error_reporting(0); should turn all errors off. http://php.net/manual/en/function.error-reporting.php – Jason K May 24 '16 at 16:44
  • @JasonK error_reporting can be modified using ini_set. It's in the "Examples" section on the PHP manual page you linked – user3163495 May 24 '16 at 16:50
  • **Why you fixed those without hidden it !!** – Abdulla Nilam May 24 '16 at 17:04
  • Fix those errors. So you no need to hide those. Its best – Abdulla Nilam May 24 '16 at 17:08
  • @Abdulla I intentionally put the error in the code I posted so that I could better understand PHP error-handling mechanism. Also, the point of it is to hide *unknown* errors from users that you, the developer, have not caught yet – user3163495 May 24 '16 at 17:15

2 Answers2

2

After trial and error and using ShiraNai7's explanations I have found that this is a bug in PHP on Microsoft IIS and FastCGI. See link to bug:

Bug #44729 display_errors = Off not respected

https://bugs.php.net/bug.php?id=44729

user3163495
  • 2,425
  • 2
  • 26
  • 43
1

Yes, PHP errors and their configuration may seem a bit really weird if one isn't familiar with the inner workings. There is a lot going on.

display_errors

  • controls whether PHP's default error handler prints errors to the output as they happen
    • error codes suppresed by error_reporting will not printed
  • controls whether PHP's default exception handler prints uncaught exceptions to the output

log_errors

  • controls whether PHP's default error handler logs errors to error_log
    • error codes suppresed by error_reporting will not be logged
  • controls whether PHP's default exception handler logs exceptions to error_log

error_reporting

  • defines a global mask for errors that should be reported
  • this option is used by the PHP's default error handler (see above)
  • this option should be used by the custom error handler too

set_error_handler()

  • defines a custom error handler
  • it is always called if an error occurs (even if its suppressed)
  • if it returns false, the PHP's default error handler WILL be called (and print / log errors according to the settings)
  • if it returns null or true or throws an exception, the PHP's default error handler WILL NOT be called (and thus it won't print / log anything).

set_exception_handler()

  • defines a custom exception handler
  • PHP's default exception handler will not be called if a custom handler is defined (and thus it won't print / log anything)

But, why even bother with "display_errors = 'Off'" when it does not have an effect anyway?

There are combinations of the above settings that make display_errors not useless.

Examples:

  • display_errors must be off in php.ini on production servers to prevent leaking of possibly sensitive data
    • an error / exception can happen before you have the chance to change any of the other settings
  • if you want to log errors but not print them to the output, you set display_errors to off and log_errors to on
  • if you are using a custom error handler that prints custom error messages but you also want to use error_log, you set display_errors to off
Shira
  • 6,392
  • 2
  • 25
  • 27
  • Sorry for my slowness...I'm still trying to wrap my head around it. Could you show me a short example (in code) where "display_errors = Off" would have an effect on the error output? – user3163495 May 24 '16 at 17:04
  • Iirc `display_errors` outputs the error directly as part of the output, if the type isn't suppressed. Thus one should set it to Off in production. – Charlotte Dunois May 24 '16 at 17:20
  • @user3163495 I added more info to the answer and provided example cases where `display_errors` has an effect. – Shira May 24 '16 at 18:07
  • @ShiraNai7 have you tried testing the code that I posted? My example contradicts your statement "if you want to log errors but not print them to the output, you set display_errors to off" – user3163495 May 24 '16 at 19:02
  • @user3163495 I did and it doesn't show any errors. Are you by any chance using a custom error handler? The error message you are getting doesn't look like it was produced by PHP. – Shira May 24 '16 at 19:24
  • @ShiraNai7 I have not set up a custom error handler. Where would I check if one existed, if it's not in the code? Can they exist in php.ini? – user3163495 May 24 '16 at 19:29
  • @user3163495 Try calling `var_dump(set_error_handler('var_dump'));` and see what it returns. It should return `NULL` if there was no custom error handler. – Shira May 24 '16 at 19:36
  • @ShiraNai7 I replaced line 9 with "var_dump(set_error_handler('var_dump'));" It returned NULL. See image: http://i.imgur.com/tOz9TTQ.png – user3163495 May 24 '16 at 19:38
  • @user3163495 Tried it through a browser (PHP 5.6) and through console (PHP 5.3, 5.4, 5.5, 5.6 & 7.0) - it just outputs "Hey". See https://3v4l.org/NYsRA (though it looks like HHVM doesn't support this setting). – Shira May 24 '16 at 19:55
  • @ShiraNai7 Thanks for the analysis. Do you think it has something to do with me using Microsoft IIS and fast CGI? – user3163495 May 24 '16 at 20:13
  • 1
    @ShiraNai7 This turned out to be a bug ShiraNai7 – user3163495 May 25 '16 at 13:30