SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple problem????

    Ok! I got 5 variables, $option1, $option2 etc.. I want to do the same sort of thing on each variable, so thought I would use a while loop. So I tried the following.

    while ($count <= 5) {
    $optionx = '$option' . $count;

    if ($optionx == true) {
    // code
    }
    $count++;
    }

    However while wondering why this didn't work I put
    echo "OptionX = $optionx"; and it printed OptionX = $option1 not the value of option1 which I was hoping.

    Hopefully you can see what I want to do and what the problem is, any help / tips would appricated.

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're trying to rename a variable name... you need to use variable variables.

    On the other hand are you familiar with the array_walk function? its very useful in these situations.

    You can simply put all your options in an array.

    Then

    function function_name($element){
    //Do whatever with element
    }

    array_walk($option_array, 'function_name');

    array_walk will pass each value in the array to the function, so you can do the same thing on each element no problem.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh... here is more info on variable variables

    http://www.php.net/manual/phpfi2.php#varvars
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  4. #4
    SitePoint Enthusiast Stallion's Avatar
    Join Date
    Jan 2001
    Location
    Cumberland, RI, US
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As aspen said, variable variables is what you need.
    PHP Code:
    for ($i=1;$i<=5;$i++) // will use 5 variables, from 1-5
    ${"option".($i-1)} = ${"option$i"}; // $option0 = $option1; $option1 = $option2; etc.... 


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
  •