Thank you curly brackets!
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #1
Thank you curly brackets!
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:

Code:
Array
(
	[test] => Test
	[message] => Say Hi
) 


The file sayhi.txt contains:

Code:
Hey there Mr. {$test}, please {$message}!


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.

PHP Code:
output_data('sayhi', array('test' => "Test", 'message' => "Say Hi");


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 Tongue

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
function output_data($name, $vars=array())
{
	global $var1, $var2; // imagine having variables here for each page, it would become really big!

	foreach ($vars as $key => $value)
	{
		${$key} = $value;
	}

	// ... rest of the function
}


There you have it Tongue

A big story with a short ending lol.

(This post was last modified: 07-14-2010 04:01 AM by Pirata Nervo.)
07-14-2010 04:00 AM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

Messages In This Thread
Thank you curly brackets! - Pirata Nervo - 07-14-2010 04:00 AM

 Standard Tools
Forum Jump: