PHP Switch function sort of working.

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
jagger27
Forum Newbie
Posts: 2
Joined: Sat Feb 28, 2009 12:55 am

PHP Switch function sort of working.

Post by jagger27 »

Code: Select all

$p = $_GET["p"];
switch($p): 
    case "0": echo "zero"; break;
    case "1": echo "one"; break;
    case "2": echo "two"; break;
    case "3": echo "three"; break;
    case "4": echo "four"; break;
    case "5": echo "five"; break;
    case "6": echo "six"; break;
    default: echo "This is the formal page. There are 7 different pages that you can look at from 0 to 6. (zero being the first)";
endswitch;
In the URL, when I go to example.com/index.php?p=0 it works perfectly with no errors. This is the same with 1 to 6. It's just when I go to index.php it gives me this error:

Code: Select all

Notice: Undefined index: p in C:\wamp\www\php\switch-function.php on line 21
Under that, it prints what I want it to say. Any solution?
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: PHP Switch function sort of working.

Post by susrisha »

put this at the start of your code

Code: Select all

 
if(!isset($_GET["p"]))
{
//your default error message
}
else
{
//your present code
}
 
jagger27
Forum Newbie
Posts: 2
Joined: Sat Feb 28, 2009 12:55 am

Re: PHP Switch function sort of working.

Post by jagger27 »

Thanks!
Post Reply