SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    nyc
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what syntax for a variable name that contains a variable?

    Hi all

    I want to use a variable whose name contains a variable! I don't know how to do that.

    For example, my variable name is

    $var_1

    but I want to increment the '1' in the variable name, throughout the script.

    something like

    $var_{++$n}

    but this doesn't work.
    Then, I want to be able to reference to that variable within a function, for example:

    function dosomething(){
    global $var_{$n};
    if ($var_{$n} == "$var_6"){
    blabla;
    }
    }

    but I can't get the syntax right

    I need help!

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are looking for variable variables

    PHP Code:
    $var 'var_1';
    $
    $var 'foo';
    ++
    $var;
    $
    $var ' bar';
    echo 
    $var_1$var_2
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sebnewyork

    It's better to use arrays for that.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    nyc
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that helped.
    Now, is it possible to retreive a variable variable with
    $_GET[''];

    Say, I am using something like:
    <?php
    $foo = 'bla';
    $n = 0;
    ++$n;
    echo'<a href="page.php?var'.$n.'='.$foo.'">link</a>';

    How can I then $_GET['var'.$n] ???

  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WEll, either you should know what the variable is or you use regular expression like
    PHP Code:
    $i=0;
    foreach(
    $_GET as $key=>$value)
    {
      
    $i++;
      if(
    preg_match('~var([0-9]+)~,$key,$match)) //TO GET JUST THE NUMBER
      {
         echo "<pre>--$i--";
          print_r($match);
         echo '
    </pre>';
      }
      if(preg_match('
    ~(var[0-9]+)~,$key,$match))//FOR the whole name
      
    {
         echo 
    "<pre>--$i--";
          
    print_r($match);
         echo 
    '</pre>';
      }

    This is not tested. logic is correct, not sure about syntax
    ---------------------------
    Errors = Improved Programming.
    My Site

  6. #6
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm in agreement with stereofrog. I think you might want to consider arrays for this type of logic.

    For example, each element in your array could represent one of your variables, and you could have:
    PHP Code:
    $my_var_array = array();
    if (isset(
    $my_var_array[5])) //to check the 6th variable, since arrays are 0 indexed. 
    Then, for printing your links:
    PHP Code:
    for ($i 0$i count($my_var_array); $i++)
    {
       echo(
    "<a href='link.php?var" . ($i 1) . "=" my_var_array[$i] . "'>your link</a>");


  7. #7
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    nyc
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you all


Bookmarks

Posting Permissions

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