Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,744
    Thanks
    29
    Thanked 4 Times in 4 Posts

    Problem defining a variable

    Hi,

    I am trying to get the contents of a variable as the
    variable changes in a loop.

    This is what I mean:

    My Code:
    PHP Code:
    //These are the variables with the data:

    $Db_plat_name1"Platform Name 1";
    $Db_plat_name2"Platform Name 2";
    $Db_plat_name3"Platform Name 3";
    $Db_plat_name4"Platform Name 4";
    $Db_plat_name5"Platform Name 5";
    $Db_plat_name6"Platform Name 6";
    $Db_plat_name7"Platform Name 7";
    $Db_plat_name8"Platform Name 8";
    $Db_plat_name9"Platform Name 9";
    $Db_plat_name10"Platform Name 10";

    // now the loop

    for ($plats 1$plats <= 10$plats++) {

      
    $Db_plat_name = ${ 'Db_plat_name'.{$plats} };

      echo 
    "This Db_plat_name: $Db_plat_name";
      } 

    I get an error :

    PHP Parse error: syntax error, unexpected '{' in myscript.php on line 48
    How should I format that variable name ?

    EDIT ...


    OK FIXED IT ... $Db_plat_name = ${ 'Db_plat_name'.$plats};

    .
    Last edited by jeddi; 08-21-2013 at 01:37 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  2. #2
    Senior Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    1,024
    Thanks
    184
    Thanked 127 Times in 127 Posts
    PHP Code:
    $Db_plat_name1"Platform Name 1";
    $Db_plat_name2"Platform Name 2";
    $Db_plat_name3"Platform Name 3";
    $Db_plat_name4"Platform Name 4";
    $Db_plat_name5"Platform Name 5";
    $Db_plat_name6"Platform Name 6";
    $Db_plat_name7"Platform Name 7";
    $Db_plat_name8"Platform Name 8";
    $Db_plat_name9"Platform Name 9";
    $Db_plat_name10"Platform Name 10";

    // now the loop

    for ($plats 1$plats <= 10$plats++) {

      
    $Db_plat_name = ${ 'Db_plat_name'.$plats};

      echo 
    "This Db_plat_name: $Db_plat_name";
      } 
    output (formatted)
    This Db_plat_name: Platform Name 1
    This Db_plat_name: Platform Name 2
    This Db_plat_name: Platform Name 3
    etc
    0000

  3. #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,867
    Thanks
    3
    Thanked 454 Times in 444 Posts
    You should actually be using an array for this.

    Using variable variables takes 3 times longer to reference (set or read) the value than using an array and your code must now know how many values there are (the 10 in your loop counter.)

    If you use an array, the code will be faster and you can simply use a foreach(){} loop, so it doesn't matter how many there are. You can simply change how many values you get from wherever these are being defined/input at and you don't need to find and change any loop counters because there won't be any counters to keep track of.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  4. #4
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,786
    Thanks
    31
    Thanked 931 Times in 928 Posts
    CFMaBiSmAd is 100% right and I love to code so:
    PHP Code:
     <?php
    $names 
    = array('Platform Name 1',
    'Platform Name 2',
    'Platform Name 3',
    'Platform Name 4',
    'Platform Name 5',
    'Platform Name 6',
    'Platform Name 7',
    'Platform Name 8',
    'Platform Name 9',
    'Platform Name 10');

    foreach(
    $names as $things){
        echo 
    $things '<br />';
    }
    ?>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


 

Posting Permissions

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