2012-03-26 85 views
-7

可能重複:
PHP: 「Notice: Undefined variable」 and 「Notice: Undefined index」代碼拋出警告:未定義指數在GET數組

此代碼顯示了*標記行警告,因此無需使用任何ini_set("display_errors", 0);想法? 只需要任何備用碼...

$page = $_GET['page']; // gets the variable $page ****** 
//global $page; 

if (!empty($page)){ 
    include($page); 
} // if $page has a value, include it 
else { 
    include("home.php"); 
} // otherwise, include the default page 

這裏是警告 「通知:未定義指數:頁:\ WWW \ Apachi_xampp \ SETUP \ XAMPP \ htdocs中\的index.php上線284」

在此先感謝。

+2

請用更直觀的問題標題:) – Spyros 2012-03-26 07:05:29

+0

什麼是錯誤/警告你?你的問題真的會被粉碎。 – gideon 2012-03-26 07:07:00

+0

這是一個很好的問題。標題很好。問題就像他說的:$ _GET ['page']將返回一個警告,如果在'_GET中找不到'page'索引。所以解決方案是在嘗試檢索值之前測試它是否已設置。最快的方法可能是使用Telulz或Kuipers建議的三元條件。 – Stefan 2012-03-26 07:27:32

回答

1

它,因爲如果頁面paramater沒有在URL中設置它會拋出一個錯誤?用下面的代碼替換:

$page = (isset($_GET['page'])) ? $_GET['page'] : ''; // gets the variable $page ****** 
//global $page; 

if (!empty($page)){ 
    include($page); 
} // if $page has a value, include it 
else { 
    include("home.php"); 
} // otherwise, include the default page 
4

你確定這是一個警告,而不是通知說「身份不明的索引」? 試試這個:

$page = (isset($_GET['page']) ? $_GET['page'] : ''); 
+0

非常感謝,我真的很喜歡:) – 2012-03-26 07:25:21

0
if(isset($_GET['page'])){ 
    $page = $_GET['page']; 
    if(!empty($page)){ 
     include($page); 
    }else{ 
     include("home.php"); 
    } 
}