Re: Using a variable to call another variable

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

 



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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']);
> 
> tedd

class myClass {
	private var $_001;
	private var $_002;
	private var $_003;

	public function access_var($var) {
		return $this->$$var;
	}
}

$cs = new myClass;
$cs->access_var('_001');

// Done

BTW, please make certain that you aren't really naming your variables as
$001, $002 and $003. Those are bad variable names, as PHP only allows
for variables beginning with letters and '_' characters (what I did above).

- --
Christopher Weldon, ZCE
President & CEO
Cerberus Interactive, Inc.
cweldon@xxxxxxxxxxxxxxxxxx
979.739.5874
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFAsfXZxvk7JEXkbERAiYkAJ9misO/pDJYEpJM3iPFF5T3GVdKGwCgpwFB
ae17qOdSZL2DJj+VA6rUqDc=
=dRAJ
-----END PGP SIGNATURE-----

-- 
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