Can Variables be Put Inside Variables?

Discussion in 'PHP' started by Masterful, Oct 3, 2008.

  1. #1
    When someone clicks a link on my site, they are sent to a page which:

    • adds the click to a database
    • selects the real link
    • redirects the visitor using the real link

    mysql_query("UPDATE table SET column = column+1 WHERE id=$id"); 
    $href = "SELECT link FROM table WHERE id=$id";
    $qry = mysql_query($href);
    list($href)=mysql_fetch_row($qry);
    
    header("Location:$href\n");
    PHP:
    The problem is, my real links ($href) contain a variable, too:

    http://www.example.com?id=$var

    Will having a variable ($var) inside another variable ($href) work?
     
    Masterful, Oct 3, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    It won't work. You need to use eval:

    
    header('Location: '.eval($href));
    
    PHP:
    Peace,
     
    Barti1987, Oct 3, 2008 IP
    Masterful likes this.
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks for the response! Points added to your account. :)

    One quick thing though . . . Are you absolutely sure about this? It's very important that I get this right first time round.

    And why did you get rid of "\n". Can't I use that in there?

    header("Location: " .eval($href\n));
    PHP:
     
    Masterful, Oct 3, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Always test before implementation:

    
    $href = 'page.php?id=$test';
    $test = '23';
    
    echo eval($href);
    
    PHP:
    Why would you need a newline character at the end? After the location header, the page changes so you won't be able to send other headers.

    Peace,
     
    Barti1987, Oct 4, 2008 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    if all you need to do is append an id to the data pulled from the DB, why can't you just do $href .= "?id=$var" or is the output from the DB already going to be referencing it from the DB? i have to say such an approach as the eval suggested here is far from practical and I believe you may simply not be communicating what you are trying to achieve correctly--unless you REALLY record URLs like '/page.php?id=$var' in there.

    its far more practical to return just 'page.php' and then add the param afterwards or append within the header function itself.

    and yes, you can have whatever combos of variables within variables:

    $foo = "bar";
    $bar = "foo$bar"; // foobar

    make sure you use double quotes " - with single ' it will be literal and just make it print the 'foo$bar'.
     
    dimitar christoff, Oct 4, 2008 IP
  6. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Doesn't work, dude:

    Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\xampp\ctyi\testing.php(7) : eval()'d code on line 1
    Code (markup):
    However, I got it to work like this:

    <?php
    
    $href = 'page.php?id=$test';
    $test = '23';
    
    eval("\$href = \"$href\";");
    
    echo $href;
    
    ?>
    PHP:
    Thanks for the guidance!
     
    Masterful, Oct 4, 2008 IP
  7. bm_ys

    bm_ys Well-Known Member

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    131
    #7
    you wil use your own type of variables int href and replace them with original ones for example

    $href = 'http://www.example.com/index.php?id=[id]&p=[p]&s=';
    $href =str_replace('[id]' , $id , $href);
    $href =str_replace('[p]' , $p , $href);
    $href =str_replace('' , $s , $href);

    now
    header("Location:$href\n");
    will wrok
     
    bm_ys, Oct 5, 2008 IP