View Full Version : Resolved variable variables in a loop

09-14-2011, 04:47 PM
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:

foreach ($lines as $line_num => $line) {
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):


What did I wrong?

If I simply create it like:

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...

09-14-2011, 04:49 PM
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?

09-14-2011, 05:19 PM
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:

$__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

09-14-2011, 05:43 PM
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...

09-14-2011, 09:18 PM
It doesn't work because your variables are scoped locally to the loop. You need to declare them with global (http://php.net/language.variables.scope#language.variables.scope.global) or use something like this to assign them:

$GLOBALS[$var_name'] = $var[1];

09-15-2011, 10:17 AM
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. :D