-7

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

This code shows warnings on the * marked line, so any idea without using ini_set("display_errors", 0); ? Just need any alternate code...

$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

And here is the warning "Notice: Undefined index: page in :\www\Apachi_xampp\setup\xampp\htdocs\index.php on line 284"

Thanks in advance.

Community
  • 1
  • 1
Cool Brain
  • 48
  • 4
  • 2
    please use a more intuitive question title :) – Spyros Mar 26 '12 at 07:05
  • What are the errors/warnings you get? Your question is really going to get shredded. – gideon Mar 26 '12 at 07:07
  • It's a good question. The title is fine. The problem is like he says: $_GET['page'] will return a warning if 'page' index is not found in $_GET. So the solution is to test if it is set before attempting to retrieve the value. Fastest way is probably to use ternary condition like Telulz or Kuipers suggest. – Stefan Mar 26 '12 at 07:27

3 Answers3

4

Are you sure it's a warning and not a NOTICE saying "unidentified index"? Try this:

$page = (isset($_GET['page']) ? $_GET['page'] : '');
Rick Kuipers
  • 6,616
  • 2
  • 17
  • 37
1

Its because if the ?page paramater isn't set in the URL it will throw that error. Replace with the following code:

$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
Menztrual
  • 40,867
  • 12
  • 57
  • 70
0
 if(isset($_GET['page'])){ 
     $page = $_GET['page'];
     if(!empty($page)){
       include($page);
     }else{
       include("home.php");
     }
 }
SuperNoob
  • 382
  • 3
  • 6