“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

后端 未结 28 910
盖世英雄少女心
盖世英雄少女心 2021-01-24 14:18

I\'m running a PHP script and continue to receive errors like:

Notice: Undefined variable: my_variable_name in C:\\wamp\\www\\mypath\\index.php on line 10

28条回答
  •  失恋的感觉
    2021-01-24 14:54

    In reply to ""Why do they appear all of a sudden? I used to use this script for years and I've never had any problem."

    It is very common for most sites to operate under the "default" error reporting of "Show all errors, but not 'notices' and 'deprecated'". This will be set in php.ini and apply to all sites on the server. This means that those "notices" used in the examples will be suppressed (hidden) while other errors, considered more critical, will be shown/recorded.

    The other critical setting is the errors can be hidden (i.e. display_errors set to "off" or "syslog").

    What will have happened in this case is that either the error_reporting was changed to also show notices (as per examples) and/or that the settings were changed to display_errors on screen (as opposed to suppressing them/logging them).

    Why have they changed?

    The obvious/simplest answer is that someone adjusted either of these settings in php.ini, or an upgraded version of PHP is now using a different php.ini from before. That's the first place to look.

    However it is also possible to override these settings in

    • .htconf (webserver configuration, including vhosts and sub-configurations)*
    • .htaccess
    • in php code itself

    and any of these could also have been changed.

    There is also the added complication that the web server configuration can enable/disable .htaccess directives, so if you have directives in .htaccess that suddenly start/stop working then you need to check for that.

    (.htconf / .htaccess assume you're running as apache. If running command line this won't apply; if running IIS or other webserver then you'll need to check those configs accordingly)

    Summary

    • Check error_reporting and display_errors php directives in php.ini has not changed, or that you're not using a different php.ini from before.
    • Check error_reporting and display_errors php directives in .htconf (or vhosts etc) have not changed
    • Check error_reporting and display_errors php directives in .htaccess have not changed
    • If you have directive in .htaccess, check if they are still permitted in the .htconf file
    • Finally check your code; possibly an unrelated library; to see if error_reporting and display_errors php directives have been set there.

提交回复
热议问题