Milos Savic Milos Savic - 2 months ago 7
PHP Question

Undefined index page if get page

I cant find specific answer for my problem. So what I'm trying to do is to define index page by using only index.php. This is my code so far:

<?php
if(isset($_GET['page'])){

if(!$_GET['page']){
include "template/home.php";
}

if($_GET['page']=="gallery"){
include "template/gallery.php";
}
}
?>


When I'm trying to open it on local host there is nothing only white screen without any errors. Only thing that makes me problem is this string, but still without any errors, can't find what problem is...

if(!$_GET['page']){
include "template/home.php";
}


I was trying to find on internet solution, but there was nothing helpful, hope u can help me :)

Answer

The issue is that you're contradicting yourself in your code. Your example reads like this: "If page is set, proceed. Then, if page is not set, include the home page, if page is gallery, then include the gallery page." Do you see the issue? The isset() check will ensure that the code with the negation operator ! is not run.

The solution is to move that code outside of the primary if block.

Comments