2012-07-30 39 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級別來忽略它們。