Change variable name
Sorry for confuzing subject, but that's the thing I'm trying to achieve. For
example:
$a = "Hello";
$b = 1;
I want to change the name of $a to $a1 where 1 is value of variable $b!
Thanks for all the help,
Boris
Re: Change variable name
Hi!
If is intended to be used in a "larger scale" you might wanna consider
implementing it as an array. You can then use the $a value as the key.
At least that is one way to do it... :-)
/Michael
Boris Savc wrote:
> Sorry for confuzing subject, but that's the thing I'm trying to achieve.
> For example:
>
> $a = "Hello";
> $b = 1;
>
> I want to change the name of $a to $a1 where 1 is value of variable $b!
>
> Thanks for all the help,
> Boris
>
Re: Change variable name
"Boris Savc" <boris.savc [at] siol.net> wrote in message
news:Xf%Wh.741$553.592020 [at] news.siol.net...
> Sorry for confuzing subject, but that's the thing I'm trying to achieve.
For
> example:
>
> $a = "Hello";
> $b = 1;
>
> I want to change the name of $a to $a1 where 1 is value of variable $b!
>
> Thanks for all the help,
> Boris
>
>
$a = Hello";
$b=1;
if($b==1)
{
$a1=&$b;//creating $a1 as a reference and assigning it's value to the $b
variable.
}
echo $a1;//output is Hello
The code above creates a variable $a1 that is in fact a reference to $a; It
is important to know that if you change the value of $a you will also change
the dereferenced value of $a1, and visa versa. In other words, if you
change the value of $a after the code above you will get the same result
from $a1.
You are not renaming $a actualy. That isn't possible. But you are creating
a variable $a1 that is actualy tied, or bound, to $a. From that point
onward they effectively share the same values and behave as if they are the
same variable.
HTH
Vince
Re: Change variable name
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:462c8594$0$16555$afc38c87 [at] news.optusnet.com.au...
> "Boris Savc" <boris.savc [at] siol.net> wrote in message
> news:Xf%Wh.741$553.592020 [at] news.siol.net...
> > Sorry for confuzing subject, but that's the thing I'm trying to achieve.
Having seen another answer to this q in another group I don't think I
understood this question at all.
Re: Change variable name
"Boris Savc" <boris.savc [at] siol.net> wrote in message
news:Xf%Wh.741$553.592020 [at] news.siol.net...
| Sorry for confuzing subject, but that's the thing I'm trying to achieve.
For
| example:
|
| $a = "Hello";
| $b = 1;
|
| I want to change the name of $a to $a1 where 1 is value of variable $b!
either use $a as an array having the keys be the differring values of $b as
someone suggested, or get into the magical, mystical world of shitty
development practices such as:
$${'a' . $b};
that makes for VERY easily maintained code since it shows exactly where a
variable comes from, what role it plays in your code, it's type, etc., etc.
knock yourself out! all i can say is that i hope you turn pro using code
like that. means i will get a raise. :)
(sorry to sound snippy with you. i'm just playing. i'm being extreme so that
the point is driven home to you that, although it can be done, it's not a
good idea...either way, the above is how you can do it.)
Re: Change variable name
Post removed (X-No-Archive: yes)
Re: Change variable name
"Tom" <tom [at] to.com> wrote in message news:f0is91017bv [at] drn.newsguy.com...
| On Mon, 23 Apr 2007 11:46:31 +0200, Boris Savc wrote...
| >
| >Sorry for confuzing subject, but that's the thing I'm trying to achieve.
For
| >example:
| >
| >$a = "Hello";
| >$b = 1;
| >
| >I want to change the name of $a to $a1 where 1 is value of variable $b!
| >
| >Thanks for all the help,
| >Boris
| >
| >
|
|
| Probably have to use a hash/associative array where you can make up
whatever key
| name you need to store its value.
nope...
${'a' . $b};
works just fine...though not at all advisable. ;)