SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about variable variables

    Hey I have some variables that differ just for a single digit, like this:

    PHP Code:
    $variable1 '';
    $variable2 ''
    I was thinking about putting them inside a for loop to number them automatically with the loop index, is this possible? I mean something like this:

    PHP Code:
    for ($i=1$i<10$i++) {
      
    $variable$i '';

    This of course doesn't work, so I've also tried writing

    PHP Code:
    $variable{$i} = ''
    but it doesn't work either.

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    664
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual/en/languag...s.variable.php
    Be aware that in most cases you will probably end up using an array anyways.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What do the variables actually contain? Are they variables you are going to put in a database table?

  4. #4
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    http://www.php.net/manual/en/languag...s.variable.php
    Be aware that in most cases you will probably end up using an array anyways.
    Thank you, but as I've shown in my examples, I can't make it work

    Quote Originally Posted by Cups View Post
    What do the variables actually contain? Are they variables you are going to put in a database table?
    They contain rows extracted from a database, containing information to show to the user. I'm not using them to store anything in a database.

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,909
    Mentioned
    96 Post(s)
    Tagged
    0 Thread(s)
    If it's just the rows from a results set then your better off using a while loop:

    PHP Code:
    while ($row mysql_fetch_array($resultMYSQL_ASSOC)) {
        
    $result_set[] = $row;

    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    <?php

    $first_bit_of_variable_name 
    'woop';

    for(
    $i=0$i<5$i++) {
        
    $variable_variable_name $first_bit_of_variable_name.$i;
        $
    $variable_variable_name 'foobar'.$i;
    }

    var_dump($woop3); // => foobar3

    ?>
    I couldn't get it to work without the extra step of concatenating the $variable_variable_name first.

    And also, more importantly, it's totally unreadable if you look at this code a month later

  7. #7
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,692
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Using an array is considerably more flexible. If you use variables, you'll have no easy way of checking the number of variables. Looping through the variables or passing them on to functions will also be quite cumbersome. With an array, this is very easy to do.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    I couldn't get it to work without the extra step of concatenating the $variable_variable_name first.
    Immerse, your example could use

    PHP Code:
    ${$first_bit_of_variable $i} = 'foobar'.$i
    The code within curly braces should evaluate to a string, which is the name of the variable. It needn't just be variables either; function calls and other operations work too, so long as it's a single statement (no semicolons).
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Say your result set from your database is retrieves logical field names like name, address.

    These are available to you as say, $row['name'] $row['address'], agreed?

    If you want to use them as named variables then you could just use the object notation instead of array notation eg

    $row->name, $row->address

    This is

    a) easier to read
    b) easier to type
    c) can be used inside double quotes

  10. #10
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    As has been said by everyone here, just use an array. You're VASTLY overthinking something simple AND trying to abuse 'variable variables' for something they really were never meant to do.

  11. #11
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C. Ankerstjerne View Post
    Using an array is considerably more flexible. If you use variables, you'll have no easy way of checking the number of variables. Looping through the variables or passing them on to functions will also be quite cumbersome. With an array, this is very easy to do.
    Yeah, I was thinking about using a for loop because I know the number of variables that I need.

    Quote Originally Posted by deathshadow60 View Post
    As has been said by everyone here, just use an array. You're VASTLY overthinking something simple AND trying to abuse 'variable variables' for something they really were never meant to do.
    Ok, thank you very much everybody!

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Cups View Post
    $row->name, $row->address

    This is

    a) easier to read
    b) easier to type
    c) can be used inside double quotes
    a) means you will find errors far more easily
    b) means your are less likely to make quoting errors

    and I should have mentioned:

    d) it introduces the idea of objects into your psyche

  13. #13
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,692
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by D3V4 View Post
    Yeah, I was thinking about using a for loop because I know the number of variables that I need.
    foreach is faster than for in looking through an array. And its syntax is prettier
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  14. #14
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C. Ankerstjerne View Post
    foreach is faster than for in looking through an array. And its syntax is prettier
    Sorry for the late reply

    Is it really faster? Why? *_* I don't know why, but I thought it was the opposite, because in a for loop I explicitly say what the upper limit is...

  15. #15
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,692
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    I just checked, and for is indeed faster (and while is faster still). Turns out the speed test I made gave foreach a significant advantage.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!


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
  •