Re: Using a variable to call another variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux