2012-03-26 86 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"); 
    } 
}