Current location: Hot Scripts Forums » Programming Languages » PHP » using variable variables with multiple variables for parts of the name


using variable variables with multiple variables for parts of the name

Reply
  #1 (permalink)  
Old 07-23-05, 10:00 PM
harlock harlock is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Question using variable variables with multiple variables for parts of the name

I'd like to figure out how to construct the name of a variable using two or more variable names.

lets say $variable1 = whee
and $variable 2 = fun

I'd like to construct a variable variable named "$wheefun"

If I try something like:

Code:
<?php

$variable1 = 'whee';
$variable2 = 'fun';

$$variable1.$variable2 = foo;

echo $wheefun;

?>
I do not get 'foo' from $wheefun, I get nothing. I don't think I have the right syntax, if it's possible at all. Not sure if the period separator works on the left side of a statement like that.

What I am doing is making a function where we build the variable name from two different variables, assign it a value from a third variable, and then using GLOBAL to make it global when I'm done. I'm doing this to take some existing variables and appending a suffix onto their name, and the suffix is determined by another variable.

There's another way I can get around it, but the cleanest, most elegant solution would involve assembling a variable name from multiple variables.

--Mike
Reply With Quote
  #2 (permalink)  
Old 07-23-05, 10:50 PM
Sabu Sabu is offline
Junior Code Guru
 
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
Not that I ever support this function, but if you really needed to, look into something like this
PHP Code:

eval("\${$variable1}{$variable2} = foo"); 

Reply With Quote
  #3 (permalink)  
Old 07-24-05, 12:15 AM
harlock harlock is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Not that I ever support this function,
Why would you not support the use of that function? Is it bad in some way? (perhaps you are trying to tell me that I should never be in a situation where I have to use it in the first place?
Reply With Quote
  #4 (permalink)  
Old 07-24-05, 12:36 AM
Sabu Sabu is offline
Junior Code Guru
 
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
A small quote from the creator of PHP:
Quote:
If eval() is the answer, you're almost certainly asking the wrong question.
Especially if you're using a form of user input to get the values of $variable1 and $variable1, someone could play around with the values and potentially cause harm to your server. What if I filled in your form so that the value of $variable2 is
Code:
meh = 0; unlink($PHP_SELF); \$$variable1.$variable2
Place that in the above code and tell me what happens.
Reply With Quote
  #5 (permalink)  
Old 07-26-05, 04:30 AM
harlock harlock is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Especially if you're using a form of user input to get the values of $variable1 and $variable1, someone could play around with the values and potentially cause harm to your server.
Right. Fortunately neither of the variables comes from user input. I'm probably going about it the wrong way, but it'd be used to generate an error flag for an option in a list of options being generated from a MySQL database. For example, If a user made a mistake when working on an entry related to foo1, it'd generate a value of 1 in a variable called $foo1error. This would be called up in multiple places, first for some error output the top of the page, and later down below in the regenerated form. I realize there are other ways of doing this and so far have avoided using eval. I just thought of it as an easy way to deal with it, but dynamic creation of variable names seems to crop up all sorts of security issues.
Reply With Quote
  #6 (permalink)  
Old 07-26-05, 04:52 AM
Sabu Sabu is offline
Junior Code Guru
 
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
You're far better off using arrays for that, use $errorarray[errorid] and check for the value of that.
Reply With Quote
  #7 (permalink)  
Old 07-26-05, 07:21 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

No need for eval().
$myFreakyVar = $var1 . $var2;
$x = &$$myFreakyVar;
$x = 'foo';
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #8 (permalink)  
Old 07-26-05, 07:35 PM
Sabu Sabu is offline
Junior Code Guru
 
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
Dennis

The point was to dynamically create the variable 'myFreakyVar' without at first knowing what it is to be called.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
formmail problem gscraper Perl 12 08-27-04 03:06 AM
PAssing variables dbalka Perl 3 10-29-03 05:00 PM


All times are GMT -5. The time now is 06:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.