Not sure if we're allowed to post samples on this forum (since its description says we can ask questions here but not that we can share samples).
Anyway, here it goes.
I've just spent about 30 minutes (probably a little bit more) trying to figure out how the hell I could use a string as a variable name.
Imagine you have three pages: index.php, login.php and sayhi.php
All of them make use of a certain function named output_data() which accepts two arguments: string $name and array $vars.
The $name variable would be used to load a file called $name.txt which would contain text with variables ({$variable} for example) wanting to be evaluated with eval().
Imagine you wanted to pass 'sayhi' as the string and the following array as the second argument of the function:
The file sayhi.txt contains:
And you'd then want to eval a string which contained two vars: $test and $message. The eval() would be run inside the output_data() function.
Now imagine you have three pages: index.php, login.php and sayhi.php
All of them make use of this function and the function is completely dynamic. That is, it must work with every file you choose.
However, in order to get it to work, we must either set the values of (for example) $test and $message before we run the function and add global $test, $message; inside the function or code the function to make it declare the variables we pass in the second argument for them to be evaluated when eval() is run.
Making them globals wouldn't be so nice because if the number of pages increases, the number of globals will also increase and thus, having a huge list of variables being "globalized" inside the function is practically something awful and definitely not recommended - plus it wouldn't be dynamic.
So, yeah the dynamic way seems to be the best in this case.
We tell the function here to declare $test with value "Test" and $message with value "Say Hi", or at least we want to tell that to the function.
How do we make the function to interpret this?
Simple! Or maybe not so simple until you understand how to do it
There you have it
A big story with a short ending lol.