Simple if else wont work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jpaytoncfd
Forum Newbie
Posts: 4
Joined: Tue Feb 15, 2011 10:56 am

Simple if else wont work

Post by jpaytoncfd »

I have this code:

Code: Select all

    if (!empty($_GET['page'])){
		$page= "Home";
	}else{
		$page= $_GET['page'];
	}
but even if GET has a value it still goes with the IF
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Simple if else wont work

Post by McInfo »

Think about what "not empty" means.
jpaytoncfd
Forum Newbie
Posts: 4
Joined: Tue Feb 15, 2011 10:56 am

Re: Simple if else wont work

Post by jpaytoncfd »

I'm just learning PHP so I'm not quite sure what you mean. I assume empty is the wrong thing to use?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Simple if else wont work

Post by Jonah Bron »

Here's your code English:

[text]if $_GET['page'] is NOT empty, set $page to home.
if $_GET['page'] IS empty, set that as the page[/text]

Obviously, it's backwards. Remove the exclamation mark. The exclamation mark means "not". You want to go to home if it IS empty.
gully_mi_seh
Forum Newbie
Posts: 14
Joined: Fri Mar 18, 2011 8:48 pm

Re: Simple if else wont work

Post by gully_mi_seh »

by the way this not safe to do it this way because the visitors can modifier the url value so as it for the GET. You should make an array whom content the accepted values, then check if it is a correct value with in_array(). If it is not a element of the array you'll set it up to "home".
Post Reply