可能重复:
PHP:“注意:未定义变量”以及“注意:未定义索引”
我在localhost中升级/安装PHP 5.4.4
(示例1.8.0)。现在我在我的页面上看到了许多notice error
。什么问题?如何解决这个问题?
错误区间:
Notice: Undefined index: language in C:'xampp'htdocs'tube'include'config.php on line 93
Notice: Undefined variable: max_avatar_width in C:'xampp'htdocs'tube'include'lang'english.php on line 495
Notice: Undefined variable: max_avatar_height in C:'xampp'htdocs'tube'include'lang'english.php on line 496
Notice: Undefined index: USERID in C:'xampp'htdocs'tube'index.php on line 26
配置PHP页面:
if ($_REQUEST['language'] != '') // <---- Line 93
{
if ($_REQUEST['language'] == 'english')
{
$_SESSION['language'] = 'english';
}
elseif ($_REQUEST['language'] == 'spanish')
{
$_SESSION['language'] = 'spanish';
}
}
if ($_SESSION['language'] == "")
{
$_SESSION['language'] = "english";
}
if ($_SESSION['language'] == "english")
{
include("lang/english.php");
}
elseif ($_SESSION['language'] == "spanish")
{
include("lang/spanish.php");
}
else
{
include("lang/english.php");
}
英语朗页(第495和496行):
$lang['491'] = "The image width is too big. Max width is $max_avatar_width pixels.";
$lang['492'] = "The image height is too big. Max height is $max_avatar_height pixels.";
索引PHP页面:
if($_SESSION['USERID'] == "") // <-- Line 26
{
$showfamfilter = "AND mature='0'";
}
elseif($_SESSION['FAMILYFILTER'] == "0")
{
$showfamfilter = "";
}
else
{
$showfamfilter = "AND mature='0'";
}
您可以将php.ini中的error_reporting
设置更改为不包括E_NOTICE
。php.ini文件中应该有一些示例。
然而,这是不明智的。。应该修复代码。例如,代替:
if ($_REQUEST['language'] != '')
你应该写:
if (isset($_REQUEST['language']))
修复所有E_NOTICE错误将使您的代码更加健壮。
归根结底,问题在于您的代码。您正在尝试引用数组项和索引,但没有首先进行初始化。
您现在看到它的原因是旧服务器禁用了E_NOTICE
以进行error_reporting。我敢打赌从未指定PHP.ini,因为默认情况下不显示错误,而是记录所有未通知和未弃用事件。
; Common Values:
; E_ALL & ~E_NOTICE (Show all errors, except for notices and coding standards warnings.)
; E_ALL & ~E_NOTICE | E_STRICT (Show all errors, except for notices)
; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors)
; E_ALL | E_STRICT (Show all errors, warnings and notices including coding standards.)
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_NOTICE | ~E_DEPRECATED
; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development, but
; it could be very dangerous in production environments. Depending on the code
; which is triggering the error, sensitive information could potentially leak
; out of your application such as database usernames and passwords or worse.
; It's recommended that errors be logged on production servers rather than
; having the errors sent to STDOUT.
; Possible Values:
; Off = Do not display any errors
; stderr = Display errors to STDERR (affects only CGI/CLI binaries!)
; On or stdout = Display errors to STDOUT
; Default Value: On
; Development Value: On
; Production Value: Off
; http://php.net/display-errors
display_errors = Off
这是一种创可贴,现在可以使用:
error_reporting( E_ALL ^ ~E_NOTICE );
如果您想在访问数组密钥之前检查它是否存在,请使用:
if(array_key_exists( 'key you are looking for', $array ) ){
....
}
在表达式中的数组中使用未定义的变量或未定义的索引将引发"未定义变量"或"未定义索引"注意。
在使用之前,您可以通过检查值是否已定义/null来避免这种情况。这样您就可以了解应用程序中的状态。
另一方面,通知和警告都可以针对异常行为(不一定是错误行为)发出。这意味着,如果您希望,可以通过设置适当的error_reporting级别来忽略它们。
在尝试访问数组索引之前,您应该检查数组索引是否存在,并确保在使用它们之前声明了任何变量。