Results 1 to 4 of 4

Thread: [resolved] If querystring exists?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Resolved [resolved] If querystring exists?

    Hello all.
    Code:
    <?php
    switch ($_GET['lang']) {
    case "es":
    	$txtGreeting = array("buenas dias");
    	break;
    case "fr":
    	$txtGreeting = array("bon jour");
    	break;
    default:
    	$txtGreeting = array("good day");
    }
    ?>
    The above code works just fine when I click on a link such as:
    Code:
    <a href="index.php?lang=fr">French</a>
    But if I click a link without the querystring (?lang=fr), I receive a nasty error message:

    "Undefined index: lang"

    Could anyone explain to me how to determine if a querystring exists BEFORE performing the switch statement?
    Or, if possible, how I could switch to default if the querystring is absent?

    Many thanks.
    Last edited by solitario; May 27th, 2005 at 02:55 PM.

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    put all that in an IF like this:
    PHP Code:
    if (isset($_GET['lang'))
        {
        
        } 
    That should check if the variable has been set at all. Find out more about the funciton here.
    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121
    Works perfectly. Thanks, Acidic!

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    It's actually not an error but a notice. There is a way you can setup the php.ini to shut off notices. I have this on my remote server, but on my test server I have it turned on, just because it helps practice better coding habits.

    If you're interested in finding knowing how to shut it off, let us know. I would encourage leaving it on, but I just thought I'd let you know there's the option.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width