Hi

I've found that my switch statement used for navigation isn't working.

http://mysite.com/test.php?go=news

should show the news but its just showing http://mysite.com/test.php. i've checked my php code and its fine:

<?

switch($go)
{
default:
echo "default";
break;
case news:
echo "news";
break;
case contact:
echo "contact";
break;
}

?>

I'm baffled!

Ant

    that doesn't look fine to me at all. your string cases aren't quoted and the first one isn't even declared as a case. Should look more like this:

    switch($go)
    {
    case "default":
    echo "default";
    break;
    case "news":
    echo "news";
    break;
    case "contact":
    echo "contact";
    break;
    }
    

      keep in mind if you're passing a variable through the URL, you may need to access it via $_GET["go"] rather than $go since register_globals are set to off by default.

        this works:

        
        <?
        
        switch($_GET['go'])
        {
        	default:
        	echo "default";
        	break;
        	case "news":
        	echo "news";
        	break;
        	case "contact":
        	echo "contact";
        	break;
        }
        
        
        ?>
        
        

        do you know why?

          Your code will work, provided you have news and contact defined as constants (somehow I doubt you do). Even if you don't, it will still work, but is still incorrect coding, and will give an error message (which you should see) about an "undefined constant". As for examancer's case "default":, somehow, again, I think just default is really what you want.

          At any rate, try some simple debugging:

          echo $go;
          switch($go) {
              default:
                  echo "default";
                  break;
              case "news":
                  echo "news";
                  break;
              case "contact":
                  echo "contact";
                  break;
          }

          I suspect this is another case of assuming that register_globals is on when it's not. It shouldn't be on, anyway, so use

          switch ($POST['go']
            Write a Reply...