Page 1 of 1

PHP Switch function sort of working.

Posted: Sat Feb 28, 2009 1:00 am
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?

Re: PHP Switch function sort of working.

Posted: Sat Feb 28, 2009 1:18 am
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
}
 

Re: PHP Switch function sort of working.

Posted: Sat Feb 28, 2009 1:28 am
by jagger27
Thanks!