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,673
    Thanks
    28
    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 12: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
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    849
    Thanks
    172
    Thanked 92 Times in 92 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
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,011
    Thanks
    2
    Thanked 312 Times in 304 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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,041
    Thanks
    23
    Thanked 589 Times in 588 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.


  •  

    Posting Permissions

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