Re: Using a variable to call another variable
| From: | Robert Cummings | Date: | Sat, 09 Sep 2006 15:19:30 +0000 |
| Subject: | Re: Using a variable to call another variable | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-241533@lists.php.net to get a copy of this message | ||
On Sat, 2006-09-09 at 08:44 -0400, tedd wrote:
> At 12:10 AM -0400 9/9/06, Robert Cummings wrote:
> >On Sat, 2006-09-09 at 12:57 +0900, Dave M G wrote:
> >> PHP List,
> >>
> >> I have a list of variables:
> >>
> >> $001
> >> $002
> >> $003
> >> $004
> >>
> >> And what I'd like to do is have a function which will select and return
> >> one of them. Something like:
> >>
> >> public function returnVar($n)
> >> {
> >> return $(somehow n is made to reference the name of the variable);
> >> }
> >>
> >> And then in later scripts I can call anyone of the variables by saying
> >>
> >> returnVar(001)
> >>
> > > Or something like that.
> >>
> >> I've been scratching my head on how to do this for a while. I thought
> >> the answer might lie somewhere in call_user_func(), but even if it is I
> >> can't determine how.
> >>
> >> Any advice would be much appreciated.
> >
> ><?php
> >
> >function easy_peasy( $name )
> >{
> > $foo1 = 1;
> > $foo2 = 2;
> > $foo3 = 3;
> >
> > return $$name;
> >}
> >
> >echo easy_peasy( 'foo2' )."\n";
> >
> >?>
> >
> >Cheers,
> >Rob.
>
>
> > Or something like that. <---- :-)
>
> $easy_peasyier = array("foo1" => 1, "foo2" => 2, "foo3"
> => 3);
>
> echo($easy_peasyier['foo1']);
That's zero marks on any exam I ever wrote. You didn't properly read the
business requirements that specified the need for a function ;) Also,
the version you showed isn't shared in any way, it has local scope, so
unless it's defined in global scope and you're working in global scope
(since I don't see a global declaration) then it's not very
accessible :))
Cheers,
Rob.
--
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com
|
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'