This is a discussion on "Any way to use a var within a predefined var?" within the PHP Forum section. This forum, and the thread "Any way to use a var within a predefined var? are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Any way to use a var within a predefined var?
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
Any way to use a var within a predefined var?
This doesn't work: $_SESSION[$var]
Is there some workaround that does. I've looked into $$vars (man, those were tough to wrap my brain a round at first!) but I don't see how they could help me. I also tried wrapping the inner var in curly brackets; that didn't work either. |
|
|
|
||||
|
Re: Any way to use a var within a predefined var?
I'm not quite sure what you're getting at. Maybe this will get you started.
Page 1:
On which page do you want to set the variable value? If I'm following you at all, you should be able to get the job done by assigning values first and putting them into the superglobal only for transmission. |
|
|||
|
Re: Any way to use a var within a predefined var?
What I'm getting at is that I want to pass a variable name (and only the variable name) to a function and have it echo back the variable name and the variable's set value...
|
|
||||
|
Re: Any way to use a var within a predefined var?
I think I see what you want.
You could almost surely do this more elegantly using variable variables, but I always pick the easy way. I'm not sure a $$ approach would actually save any coding space.
Last edited by masonbarge; May 10th, 2007 at 21:21. Reason: Tidy up |
|
||||
|
Re: Any way to use a var within a predefined var?
While I'm at it, if my first try didn't do the trick, this snippet may help you figure out what you want to do. It is really two different approaches.
|
|
|||
|
Re: Any way to use a var within a predefined var?
Awesome stuff, Mason. Thanks! I'll definitely get use out of that second example, too. I've already created functions ShowPost(), ShowGet() and ShowCookies() based on that code. Great debugging tools!
One quick question regarding the first code you posted... I "made it my own" as follows:
Thanks so much for the help! |
|
||||
|
Re: Any way to use a var within a predefined var?
$$'s -- I sympathize. It helps me to fixate on the fact that I'm naming a variable, not setting a value, then remember that when I see or use one I have to expect to take two mental steps, not one. If it really bugs you, just try setting all your variables like this for a day or two.
Using $$ is just going to rename the variable to the value of $name. It's the wrong direction. I can't think of an easy fix. If someone knows how to do this, please chime in. You can add an optional third optional argument $var which is going to get rather silly -- you'd be using it like:
If you want a more complex function definition, you might be able to set up the function you want by using variable parameters. I've never done it so I don't know how to do it or whether it would even get you where you want to go. It looks like kind of a pain but it would be fun. Last edited by masonbarge; May 11th, 2007 at 14:08. |
![]() |
| Tags |
| predefined variables, variables |
| Thread Tools | |
|
|