-4

可能重复:
PHP:“注意:未定义的变量”和“注意:未定义的索引”

我在 localhost 中升级/安装PHP 5.4.4(xampp 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'";
}
4

4 回答 4

6

您可以将error_reportingphp.ini 中的设置更改为不包含E_NOTICE. php.ini 文件中应该有一些示例。

然而,这是不明智的。你应该做的是修复你的代码。例如,而不是:

if ($_REQUEST['language'] != '')

你应该写:

if (isset($_REQUEST['language']))

修复所有 E_NOTICE 错误将使您的代码更加健壮。

于 2012-07-30T14:16:55.583 回答
1

归根结底,问题在于您的代码。您试图在未先初始化的情况下引用数组项和索引。

您现在看到它的原因是旧服务器已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 ) ){
    ....
}
于 2012-07-30T14:19:13.380 回答
1

在表达式中使用未定义变量或数组中的未定义索引将引发“未定义变量”或“未定义索引”通知

您可以通过在使用值之前始终检查值是否已定义/为空来避免这种情况。这样您就可以了解应用程序中的事物状态。

另一方面,可以针对异常(不一定是不正确)行为发出通知警告。这意味着如果您愿意,可以通过设置正确的 error_reporting 级别来忽略它们。

于 2012-07-30T14:21:44.687 回答
0

您应该在尝试访问它之前检查数组索引是否存在,并确保在使用它们之前声明了任何变量。

于 2012-07-30T14:16:28.420 回答