I am trying to accomplish something that seems as though it should be very simple, but I can't seem to find a way to make it work, despite searching a number of resources.
All I'd like to do is use the name of variable within the name of another variable. So basically I have the GET function grabbing from the URL, like this:
$tickerID = $_GET['ticker'];
Now I want to define a variable like this:
$($tickerID)1Q05 = mysql_query("blah blah blah");
that's all.... Any advice? Thanks much in advance.
Variable within a variable name
Moderator: General Moderators
Re: Variable within a variable name
how about this?
Code: Select all
$tickerID .= '1Q05';
$$tickerID = mysql_query("blah blah blah");
-
jazzercising011
- Forum Newbie
- Posts: 9
- Joined: Tue Sep 14, 2010 4:47 am
Re: Variable within a variable name
did it work?
Re: Variable within a variable name
That won't work Gargoyle. Needs to be done using curly brackets.
Code: Select all
$tickerID = '1Q05';
${$tickerID} = mysql_query("QUERY");Re: Variable within a variable name
most likely you're looking for something like ${$tickerID . "1Q05"} = 'something';
Can't say it's good idea to use such variables, especially because you don't seem to be filtering you incoming data (in $tickerID variable).
Can't say it's good idea to use such variables, especially because you don't seem to be filtering you incoming data (in $tickerID variable).
Re: Variable within a variable name
Thanks very much tim and Weirdan--works perfectly; thought I had tried that before, but must have made a syntax error.
Weirdan, what do you mean about filtering the incoming variable?
Thanks again.
Weirdan, what do you mean about filtering the incoming variable?
Thanks again.
Re: Variable within a variable name
indeed. that's weird. did PHP change its behavior for this or do I just remember it wrong? I am sure there was a time you could do this without curly braces. strange.That won't work Gargoyle. Needs to be done using curly brackets.
if you're using user input (GET data is user input), you're facing the problem of code injection, so you need to make sure the data passed by the user can't do any harm.Weirdan, what do you mean about filtering the incoming variable?