Search code examples
phpnotice

How to solve "Notice: Undefined index: ..."


how can i hide the php notice by solving the issue.

my code

if(empty($_GET['mode']) && !$_GET['page']) { 
    include($basepath.'/templates/template.home.php');
}

Notice: Undefined index: page in /var/www/public_html/index.php on line 1

i tried like this

if(empty($_GET['mode']) && !isset($_GET['page']) && !$_GET['page']) { 
    include($basepath.'/templates/template.home.php');
}

but still showing the

Notice: Undefined index: page in /var/www/public_html/index.php on line 1

how can i solve/fix it ?


Solution

  • The problem is that you use in both statements !$_GET['page'] when using && operator.

    When you use !$_GET['page'] in both cases even if you added !isset($_GET['page'] this condition is checked in $_GET['mode'] is empty.

    You simple should probably change your statement from:

    if(empty($_GET['mode']) && !$_GET['page']) { 
        include($basepath.'/templates/template.home.php');
    }
    

    into

    if(empty($_GET['mode']) && !isset($_GET['page'])) { 
        include($basepath.'/templates/template.home.php');
    }
    

    In this case simple if $_GET['page'] is not set (and of course mode is empty) you should include homepage template