开发者

What setting(s) determine whether PHP will throw an error on accessing an array using a non-existent index?

开发者 https://www.devze.com 2023-04-03 09:20 出处:网络
I\'m trying to get a PHP 5 application working on IIS7. This application appears to have been built under the assumption that accessing an array using an index that doesn\'t exist will simply return

I'm trying to get a PHP 5 application working on IIS7.

This application appears to have been built under the assumption that accessing an array using an index that doesn't exist will simply return null, without raising an error and halting the program.

Given the following code:

<?PHP
  $testarray = array();
  print $testarray[1];
  print 'hello';
?>

When accessed through Apache (installed under WampServer), it will simply print "hello" on the screen, and ignore the error.

When accessed through IIS 7, it generates the following exception:

    PHP Notice:  Undefined offset: 1 in C:\Users\Jonathan\Projects\MyApp\api\inf.php on line 3
    PHP Stack trace:
    PHP   1. {main}() C:\Users\Jonathan\Projects\MyApp\api\inf.php:0

But if I comment out the first print statement, it executes correctly and displays "hello".

Both IIS and Apache are pointing to the same code-base, using the same instance of PHP (the versi开发者_JAVA技巧on is 5.3.5) and the same PHP config.

Since this is a language issue, I don't see why it would matter what web-server I use, but I'm totally lost as to why one instance works when the other throws an error.

What setting might be responsible for this difference?

(If there's a way to configure IIS to be more lenient, I'd prefer that, rather than re-factoring the whole application to check arrays for indexes before accessing them.)


You can change this behavior by altering the error reporting settings in php.ini.

You can set different levels of error reporting. For example, trying to access an undefined array index is an E_NOTICE. You can choose whether or not to display errors of this type. To see what the other levels are, see this page.

Any production website shouldn't display any errors whatsoever, while you should probably display every error in a development environment.

0

精彩评论

暂无评论...
验证码 换一张
取 消