www.webdeveloper.com
Results 1 to 7 of 7

Thread: Assigning GET var to regular var.

Hybrid View

  1. #1
    Join Date
    Jul 2011
    Posts
    54

    Assigning GET var to regular var.

    I am having a strange issue that I think may be a syntax problem but I dont understand completely.

    Here is my code:

    PHP Code:
        if(isset($_GET['famType']))
        {
            
    $famTypeStr $famTypes[$_GET['famType']];
            echo 
    "<script>alert('famType: " $_GET['famType'] . " | " $famTypeStr "')</script>"//DEBUG
        
    }

        if(isset(
    $_GET['geo']))
        {
            
    $subgeoStr $_GET['geo'];
            echo 
    "<script>alert('geo: " $_GET['geo'] . " | " $famTypeStr "')</script>";    //DEBUG
        

    I am requesting the page containing the above code passing the get parameters as follows: ../page.php?famType="M01"&geo="2"

    The alert box pops up for both if statements and it prints the contents of the get variables as desired, but both $famTypeStr and $subgeoStr are null.

    Is there something wrong with the assignment syntax I'm using in these cases?

  2. #2
    Join Date
    Jul 2011
    Posts
    54
    One bit of info I may have forgot to add,

    the $famTypes array which is referenced in the assignment of "$famTypeStr" is defined as follows:

    PHP Code:
    $famTypes = array(
         
    "M01" => "X01 - First family",
         
    "M02" => "X02 - Second Family",
    ); 

  3. #3
    Join Date
    Aug 2006
    Posts
    1,910
    Have you tried passing the parameters without quotes, like:
    ../page.php?famType=M01&geo=2
    Dave

  4. #4
    Join Date
    Jul 2011
    Posts
    54
    That does work! Thanks!

    So in PHP is there any difference between passing an integer 2 and a string "2"?

  5. #5
    Join Date
    Aug 2006
    Posts
    1,910
    Quote Originally Posted by DaveRich View Post
    That does work! Thanks!

    So in PHP is there any difference between passing an integer 2 and a string "2"?
    I'm pretty sure when you passed it with the quotes, all 3 characters - quote 2 quote - were in the string, and of course that's not what you were expecting. Attributes in query strings aren't quoted, that's just the way it works, and they end up as strings in the $_GET array.

    Dave

  6. #6
    Join Date
    Jul 2011
    Posts
    54
    Thanks for all the help and information. It is very much appreciated.

  7. #7
    Join Date
    Jul 2009
    Location
    My house, sitting at the computer. Duh!
    Posts
    118
    You should also check that $_GET['famType'] is a key in $famTypes before attempting to use them together.

    PHP Code:
    <?php
        error_reporting
    (-1);

        
    $famTypes = array(
            
    "M01" => "X01 - First family",
            
    "M02" => "X02 - Second Family",
        ); 

        if(!empty(
    $_GET['famType']) && array_key_exists($_GET['famType'], $famTypes))
        {
            
    $famTypeStr $famTypes[$_GET['famType']];
            echo 
    "<script>alert('famType: " $_GET['famType'] . " | " $famTypeStr "')</script>"//DEBUG
        
    }

        if(!empty(
    $_GET['geo']) && !empty($famTypeStr))
        {
            
    $subgeoStr $_GET['geo'];
            echo 
    "<script>alert('geo: " $_GET['geo'] . " | " $famTypeStr "')</script>";    //DEBUG
        
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles