2012-07-30 40 views
-5

可能重复:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”PHP通知未定义的错误使用PHP 5.4+

我升级/安装PHP 5.4.4在本地主机(XAMPP 1.8.0)。现在我在我的页面上看到很多notice error。什么问题?如何解决这个问题?

Secion错误:

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'"; 
} 
+0

您能否澄清一下:您对消息“未定义的索引”和“未定义的变量”有什么不明白的地方?另外我想,这不是一个PHP5.4的问题,但你还没有正确地在你的开发环境中设置你的错误设置。 – KingCrunch 2012-07-30 14:15:18

+1

您是否想过尝试更正通知?对于第一个'注意:未定义索引',你必须测试索引是否设置等等。 – PoulsQ 2012-07-30 14:15:54

+7

为什么这么低估了,没有进一步的解释或者对这里的评论有更多的赞扬?请帮助这个人改善他的问题。 – Evert 2012-07-30 14:27:24

回答

6

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

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

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

你应该写:

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

修复所有E_NOTICE错误都会使你的代码更健壮。

+1

不正确,'isset()'只会告诉var是否被定义。它不会告诉它是否不同于'''' – PoulsQ 2012-07-30 14:18:02

+2

@PoulsQ:如果你看他的源代码,只是isset实际上会给出正确的行为。行为不同,但完整的脚本是。 – Evert 2012-07-30 14:24:52

+0

不是这个问题,你不能给出一个解决方案,只能在1个案例中工作!如果他必须在其他地方使用它,并且不明白它的作用,他会犯很大的错误! – PoulsQ 2012-07-30 14:28:11

0

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

1

在一天结束时,问题是您的代码。您正在尝试引用数组项和索引,而不是首先进行初始化。

您现在看到它的原因是,旧服务器已针对error_reporting禁用了E_NOTICE。我敢打赌,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)){ 
    .... 
} 
+0

'array_key_exists()'比'empty()'和'isset()'慢两倍。只有在您需要检查可以设置为“NULL”的数组元素时才应该使用它。 – Narf 2012-07-30 14:24:21

1

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

您可以通过始终检查该值是否在使用前定义/ null来避免此情况。这样您就可以知道应用程序中的事物状态。

另外两个通知警告可以对异常票 - 不一定是不正确 - 行为。这意味着如果你希望你可以通过设置适当的error_reporting级别来忽略它们。