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
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: Thank you curly brackets!
The braces aren't actually needed Tongue

PHP Code:
foreach($vars as $key => &$val) $$key =& $val;

Or you could do:

PHP Code:
extract($vars);


If it's for some templating system, you could just directly reference the array.  If a full templating system, I'd probably pre-parse it into .php files and reference the array that way.


My Blog
(This post was last modified: 07-14-2010 08:11 AM by ZiNgA BuRgA.)
07-14-2010 07:58 AM
Find all posts by this user Quote this message in a reply
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #3
RE: Thank you curly brackets!
(07-14-2010 07:58 AM)ZiNgA BuRgA Wrote:  The braces aren't actually needed Tongue

PHP Code:
foreach($vars as $key => &$val) $$key =& $val;

Or you could do:

PHP Code:
extract($vars);


If it's for some templating system, you could just directly reference the array.  If a full templating system, I'd probably pre-parse it into .php files and reference the array that way.


Oh, the brackets are not needed? Thanks Tongue

I don't get your last part though.
07-14-2010 10:38 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #4
RE: Thank you curly brackets!
Parse

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

into

PHP Code:
Hey there Mr. <?php echo $values['test']; ?>, please <?php echo $values['message']; ?>!


Then include the file whenever you wish to use it.
IDK if this is for MyBB or not BTW.


My Blog
07-14-2010 11:31 AM
Find all posts by this user Quote this message in a reply
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #5
RE: Thank you curly brackets!
(07-14-2010 11:31 AM)ZiNgA BuRgA Wrote:  Parse

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

into

PHP Code:
Hey there Mr. <?php echo $values['test']; ?>, please <?php echo $values['message']; ?>!


Then include the file whenever you wish to use it.
IDK if this is for MyBB or not BTW.


No it's not for MyBB, it's for a website I'm creating.

But how would you parse {$test} into <?php echo $values['test']; ?> ? With str_replace? I believe a regex would probably be faster here since the name of the variables and the amount of variables varies from file to file.

But, wouldn't it be better to have <?php echo $values['test']; ?> in the files instead of {$test}? - no replacements needed.

I'm probably not understanding you correctly Tongue

I might take a look at how wordpress does it since I want something similar.
07-14-2010 09:57 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #6
RE: Thank you curly brackets!
(07-14-2010 09:57 PM)Pirata Nervo Wrote:  But, wouldn't it be better to have <?php echo $values['test']; ?> in the files instead of {$test}? - no replacements needed.
I'm assuming you don't want the user to enter all that.  But if you're doing that, probably.

And yes, you'll need a regular expression.

My Blog
07-15-2010 08:45 AM
Find all posts by this user Quote this message in a reply
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #7
RE: Thank you curly brackets!
(07-15-2010 08:45 AM)ZiNgA BuRgA Wrote:  
(07-14-2010 09:57 PM)Pirata Nervo Wrote:  But, wouldn't it be better to have <?php echo $values['test']; ?> in the files instead of {$test}? - no replacements needed.
I'm assuming you don't want the user to enter all that.  But if you're doing that, probably.

And yes, you'll need a regular expression.

Well since it's me who's managing templates, it's fine so I guess I'll probably just use <?php ... ?>, for some unknown reason, I did not remember I could do that when I coded the function I mentioned in the first post Tongue

Thanks btw
07-15-2010 09:27 AM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: