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 6 of 6
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    Question variable variables in a loop

    I need to create dynamically some variables and give them some values starting from some strings read from a txt file.

    The problem is that, for a reason or another, I am not able to create them using a loop. My code is:
    PHP Code:
    $lines=file('languages/english.txt',FILE_SKIP_EMPTY_LINES);
    foreach (
    $lines as $line_num => $line) {
        
    $var=explode('#',$line);
        
    $var_name="__".$var[0];
        ${
    $var_name}=$var[1];
    echo 
    $var_name// this works, echo __TEST
    echo ${$var_name}; // this works echo Test
    }
    echo 
    $__TEST// this does not work 
    And my language.txt is simple (for the begining):
    Code:
    TEST#Test
    What did I wrong?

    If I simply create it like:
    PHP Code:
    $var=array('TEST','Test');
    $var_name="__".$var[0];
    ${
    $var_name}=$var[1];
    echo 
    $__TEST// This works 
    It works. But as long as I try to define the variables in a loop, it does not work any more... hm...
    Last edited by Kor; 09-15-2011 at 09:20 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Maybe it's off topic to even ask, but why aren't you just using an array for this rather than dynamically generating independent variables?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    Kor (09-15-2011)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Rowsdower! View Post
    Maybe it's off topic to even ask, but why aren't you just using an array for this rather than dynamically generating independent variables?
    It is a long story. My dictionary is, in fact, a separate php document, where the variables are defined like:
    PHP Code:
    $__WELCOME="Welcome";
    $__ADMIN="Global settings";
    $__SELECT_LANGUAGE="Select language";
    $__SIGN_OUT="Sign out"
    and so on, about a hundred of terms. The idea is that the user should switch to the desired language. This work ok. I used those global variables all over the site.

    The problem is that, at the end of the project, the client changed his mind and wants those dictionaries to be as .TXT, in a certain non standard structured format (for him to be able to modify or create new dictionaries for new languages without care about the PHP syntax)

    I don't want to enter now in all the codes to change those variable names, it will be Sisyphus' job . This is the reason for I try to create dynamically those verables names and give them those values dynamically as well
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    I see. That sounds like a fun client!

    And is there the chance that the client will add new variables to that list or no? If you're talking about these variables already being used in hard-coded ways in other PHP files then I would assume the answer is "no" - but I want to clarify...

    If no "new" variables will be declared then you already have your variable names known to you. You could leave a default PHP copy someplace with all of the PHP variable names in it and set to default values that you prefer. Or simply add those variable names to the file you are working on now manually. Then, run your script that you are trying to make here, only make it update the values for those variables to what is in the *.txt file. That's not elegant, but I think your client has foregone elegance making a change like that late in the scope of the project.

    Or, alternatively, would the client be OK with a web interface to alter/create new dictionaries? That way no syntax needs to be adhered to, but the user can still make changes or additions at will...
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    Kor (09-15-2011)

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    It doesn't work because your variables are scoped locally to the loop. You need to declare them with global or use something like this to assign them:
    PHP Code:
    $GLOBALS[$var_name] = $var[1]; 

  • Users who have thanked Inigoesdr for this post:

    Kor (09-15-2011)

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I have solved the issue. The problem (as I have later suspected) was related with the TXT file. I have saved it in UTF-8, but also with a UTF signature (BOM = Byte Order Mark), which spoiled the order of the array in a way or another.

    Thank you all, anyway! You helped me to reflect deeper about where the error could be.

    The funny thing is that, initially, my dictionaries were written as PHP variables. The client changed his mind, and he wants to be able to create his own dictionaries without care about the PHP syntax. Now, I must tell him that he will have extra care about saving in UTF-8 but without a UTF BOM signature.
    Last edited by Kor; 09-15-2011 at 09:20 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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