Results 1 to 6 of 6

Thread: Variable Value + Variable

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Variable Value + Variable

    How can I get the value of this variable to output the value of each variable? I think I might be doing this completely (I know the code is wrong but should i be using an array?). Thanks.

    I have 8 values in my page that are like this and the $t's go till 8.

    PHP Code:
    $t1 "this"
    $t2 "that"
    Incorrect code:
    PHP Code:
     for ($i 1$i $total +1$i++) {
    echo 
    $t$total;

    Last edited by bluewalrus; 01-21-2010 at 06:53 PM.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    ...you're trying to output the values of a bunch of variables?

    something like this? unless there's a good reason *not* to, I'd put them in an array.
    PHP Code:
    $tArray[1] = 'this';
    $tArray[2] = 'that';
    // etc.

    foreach($tArray as $t){
       echo 
    $t.'<br>';


  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Variable variables only are allowed for full variable names.
    $$name is fine, but $xyz$abc is an error.

    To compensate, you must use an intermediate string (annoying, but not hard):
    $name = 'xyz'.$abc;
    $$name; //now like "$xyz$abc";

    So, for the above, you just need:
    for($x=1;$x<9;$x++) {
    $name = 't'.$x;
    echo $$name;
    }
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (01-21-2010)

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    To add something to the post above, apparently there is another way around it:
    http://www.php.net/manual/en/languag...s.variable.php
    ${'xyz'.$abc} should give the same results.
    (Thanks to Nile for pointing that out-- http://dynamicdrive.com/forums/showthread.php?t=51714 )
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I tried that like this

    PHP Code:
    $contents{'p' $i}; 
    But it didn't work. I have it currently working with this

    PHP Code:
    $steps 'p' $i
    and then to call it $$steps but I can only use this once, or is there another way to recall this?
    Corrections to my coding/thoughts welcome.

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    ${'contentsp'.$}

    $a$b is confusing, as is $a{$b}

    PHP only accepts a single "argument" for the variable name, either $x or ${x}.
    In the second version it can be complex because it's within brackets.

    I'm not sure what you mean about "recalling" it. You can repeat the same code again,
    $$steps. If the value of $steps has changed by then, then you can set it again.

    Variable variables are confusing and frequently lead to problems. Almost always, arrays are a better way to go and with some good planning they are rarely needed. Occasionally, it is useful, but not that often.
    One example is when you aren't sure about which variable to use and you can dynamically choose it:
    $right = '5px'; $left = '10px'; $dir = 'left'; echo $$dir;
    (You can imagine a script in which this might be useful, though more often than not there are still better ways to get around this.)
    When it is as simple as incrementing by one, usually an array will be simpler overall.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (01-23-2010)

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
  •