For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > August 2005 > How to make a variable named after the value of another variable?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to make a variable named after the value of another variable?

2005-08-23, 6:56 pm

Hello,
How to make a variable named after the value of another variable?

I know I can use smth like:

$a="name";

$$a="hey";

and that will produce variable called $name with value "hey". But when I try
something like:


$a="_REQUEST['";
$b="']";
$c=$a."thename".$b;
$$c="thevalue";
echo $c;
foreach ($_REQUEST AS $key=>$val)
echo "$key: $val\n"

Then I have only a printout:

_REQUEST['thename']


How to make this _REQUEST['thename'] a variable name?

Ragards,

Talthen


Kimmo Laine

2005-08-23, 6:56 pm

<talthen.z-serwera.o2@nospam.pl> kirjoitti
viestissä:defeb1$980$1@nemesis.news.tpi.pl...
> Hello,
> How to make a variable named after the value of another variable?
>
> I know I can use smth like:
>
> $a="name";
>
> $$a="hey";
>
> and that will produce variable called $name with value "hey". But when I
> try something like:
>
>
> $a="_REQUEST['";
> $b="']";
> $c=$a."thename".$b;
> $$c="thevalue";
> echo $c;
> foreach ($_REQUEST AS $key=>$val)
> echo "$key: $val\n"
>
> Then I have only a printout:
>
> _REQUEST['thename']
>
>
> How to make this _REQUEST['thename'] a variable name?
>



You're shooting fish in a barrel there. You've just misunderstood something
very wrong when it comes to working with arrays. Try describing just what
exactly you are trying to do (because I can't make a head or tail out of
that. I understund the lines, but I don't see what the point of it all is...
For example after

$a="_REQUEST['";
$b="']";
$c=$a."thename".$b;
echo $c;

You say:
"Then I have only a printout:
_REQUEST['thename']"

As if it shouldn't be. That's exactly what I would expect to appear. Maybe
you meant to echo $$c. And one thing more... You cannot refer to arrays via
variable variables the way yo are trying to do it. Here's how it would work:

<?php
$my__array = array('fred' => 'flintstone', 'barney' => 'rubble');
$my__pointer = "my__array";
// Now you can refer to it like this:
echo ${$my__pointer}['fred']; // Prints "flintstone"

// But this, on the other hand, won't work:
$my_invalid_pointer = "my__array['fred']";
echo $$my_invalid_pointer; // Doesn't print anything
?>

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <eternal.erectionN0@5P4Mgmail.com>


2005-08-23, 6:56 pm

"Kimmo Laine" <eternal.erectionN0.5P@Mgmail.com>:
> As if it shouldn't be. That's exactly what I would expect to appear. Maybe


Well... I wanted it to be printed (and it is) but also I wanted the echo in
the loop (which wasn't printed).
I wanted to make an item in $_REQUEST named after the value in another
variable. Now, I know that it should be done this way:

$var="thename"
$_REQUEST[$var]="the value";

However I don't understand why this works:
$var="thename";
$$var="the value";

and when I do:
$var="$_REQUEST['thename']";
$$var="the value";
it doesn't... (and that's the way I wanted to do it in the previous post)

Regards,
Talthen



Kimmo Laine

2005-08-23, 6:56 pm

<talthen.z-serwera.o2@nospam.pl> kirjoitti
viestissä:defj3m$oe3$1@nemesis.news.tpi.pl...
> "Kimmo Laine" <eternal.erectionN0.5P@Mgmail.com>:
>
> Well... I wanted it to be printed (and it is) but also I wanted the echo
> in the loop (which wasn't printed).
> I wanted to make an item in $_REQUEST named after the value in another
> variable. Now, I know that it should be done this way:
>
> $var="thename"
> $_REQUEST[$var]="the value";
>
> However I don't understand why this works:
> $var="thename";
> $$var="the value";
>
> and when I do:
> $var="$_REQUEST['thename']";
> $$var="the value";
> it doesn't... (and that's the way I wanted to do it in the previous post)
>



Okay, I'm making a wild guess, but try without the quotes
$var=$_REQUEST['thename'];
$$var="the value";

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <eternal.erectionN0@5P4Mgmail.com>


2005-08-23, 6:56 pm

"Kimmo Laine" <eternal.erectionN0.5P@Mgmail.com>

Not working:

Not working:[color=darkred]
> Okay, I'm making a wild guess, but try without the quotes
> $var=$_REQUEST['thename'];
> $$var="the value";


Not working:
$var="_REQUEST['thename']";
$$var="the value";

Maybe the only way is $_REQUEST[$var]; ?

Regards,
Talthen


Tex John

2005-08-23, 6:56 pm

Use single quotes, not double quotes.

Also note:

http://us2.php.net/variables.predefined

Variable variables: Superglobals cannot be used as variable
variables
inside functions or class methods.
"

I can't find my book right now to be sure I remember this perfectly, but
going back to the original post, I do something like this sometimes:

$state = "texas";
${"var_state_".$state} = ".0825"; # Tax rate

gives $var_state_texas = .0825

But don't use double quotes if you want the literal name rather than the
value -- use single quotes. Or leave the $ out of the double quotes....you
don't need it.

hth
John

<talthen.z-serwera.o2@nospam.pl> wrote in message
news:defmvv$6ut$1@nemesis.news.tpi.pl...
> "Kimmo Laine" <eternal.erectionN0.5P@Mgmail.com>
>
> Not working:
>
> Not working:
>
> Not working:
> $var="_REQUEST['thename']";
> $$var="the value";
>
> Maybe the only way is $_REQUEST[$var]; ?
>
> Regards,
> Talthen
>
>



Norman Peelman

2005-08-23, 6:56 pm

<talthen.z-serwera.o2@nospam.pl> wrote in message
news:defmvv$6ut$1@nemesis.news.tpi.pl...
> "Kimmo Laine" <eternal.erectionN0.5P@Mgmail.com>
>
> Not working:
>
> Not working:
>
> Not working:
> $var="_REQUEST['thename']";
> $$var="the value";
>
> Maybe the only way is $_REQUEST[$var]; ?
>
> Regards,
> Talthen
>


Assume $_REQUEST['thename'] == 'Talthen' ...

$var = $_REQUEST['thename']; //will set $var == 'Talthen'
$$var = 'value'; //will create the variable $Talthen and set it to 'value'
echo $Talthen; // will output: value

....so...

foreach ($_REQUEST as $key => $val)
{
$$key = $val;
echo "Variable created: $key == $val \n\r";
}

would create variables from your FORM elements (everything in REQUEST
really)...

Please give an example of the imput expected and output expected.

Norm
---
FREE Avatar hosting at www.easyavatar.com



2005-08-24, 7:55 am

"Norman Peelman" <npeelman@cfl.rr.com>
This is how I solved thr problem:
$var="thename"
$_REQUEST[$var]="the value";
It creates the variable $_REQUEST['thename']="the value". I just wondered
how to do it with $$ method. However it seems that global variables cannot
be variable variables in another way, that $_REQUEST[$var]="the value";

Regards,
Talthen


Kimmo Laine

2005-08-24, 7:55 am

<talthen.z-serwera.o2@nospam.pl> wrote in message
news:dehd34$itb$1@atlantis.news.tpi.pl...
> "Norman Peelman" <npeelman@cfl.rr.com>
> This is how I solved thr problem:
> $var="thename"
> $_REQUEST[$var]="the value";
> It creates the variable $_REQUEST['thename']="the value". I just wondered
> how to do it with $$ method. However it seems that global variables cannot
> be variable variables in another way, that $_REQUEST[$var]="the value";
>



Why do you write to $_REQUEST in the first place? $_REQUEST is the array
that holds all the userinputs, so why would you want to write into that? And
also why would you use variable variables method for this, when a simple
array reference is much more simple. This makes no sense at all to me.

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com


2005-08-24, 6:55 pm

"Kimmo Laine" <eternal.erectionN05P@Mgmail.com>
> Why do you write to $_REQUEST in the first place? $_REQUEST is the array
> that holds all the userinputs, so why would you want to write into that?
> And also why would you use variable variables method for this, when a
> simple array reference is much more simple. This makes no sense at all to
> me.


Well... it's an ultra hacking method :P
I have an engine php script and some templates in php script. It is much
easier for me to change the engine, than all the templates.
All of this is working that way:
Engine1 from template1 is generating a website, I click OK and then the date
is send to engine2, which is generating the webpage using template2.
It worked fine, but I had to make new fields available in all templates used
by engine2. So it was much easier to add new $_REQUEST items in the engine2
(rest of $_REQUEST is passed from engine1) :]
It's a bit tricky, but works fine now :)

Regards,
Talthen



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com