469,357 Members | 1,648 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,357 developers. It's quick & easy.

create variables on the fly (dynamically)

bilibytes
128 100+
hi,

i was wondering how to create variables on the fly,
by this i mean: i have a pair of strings or an associative array with one string for Key and another for Value:

Expand|Select|Wrap|Line Numbers
  1. $var1 = 'hello';
  2. $var2 = 'johnny';
or --------------

Expand|Select|Wrap|Line Numbers
  1. array('hello'=>'johnny');

how could i manage to make of these two strings, a vairable with one becoming the name of the variable and the other the value.

Expand|Select|Wrap|Line Numbers
  1. echo $hello; //prints johnny

is there a thing like:

Expand|Select|Wrap|Line Numbers
  1. ${$var1} = $var2;
?


Thankyou.
Nov 6 '08 #1
5 3042
bilibytes
128 100+
The answer is to use eval()

if you want a variable to have the name of another variable's value, do :

Expand|Select|Wrap|Line Numbers
  1. $my_var_name = 'some_text'
  2. $my_value = 'some other text' 
  3.  
  4. eval( '$'.$my_var_name.' = \' '. $my_value.' \';' );
  5.  

(of course, you cannot do a variable with a wrong syntax: $myvalue ='some text'. 'some text' could never become the name of a variable, even if you write it yourself. $some text = parse error...)
Nov 6 '08 #2
Atli
5,058 Expert 4TB
Yes, you can do that.

In fact, if you combine your first example with your last example and then echo the variable $hello, you should get "johnny".

You can also do this using array key=>value pairs by using a foreach loop, like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $array = array('hello' => 'John');
  3.  
  4. foreach($array as $_key => $_value) {
  5.     ${$_key} = $_value;
  6. }
  7.  
  8. echo $hello;
  9. ?>
  10.  
Which would print "John".

I have to ask tho, why are you doing this?
It seems like sort of a backwards logic, taking arrays and importing them into variables.

Why don't you just use the arrays as they are?
Why do you want to do $hello rather than $arr['hello']?
Nov 6 '08 #3
Atli
5,058 Expert 4TB
The answer is to use eval()

if you want a variable to have the name of another variable's value, do :
No. eval is never the answer! It's very very dangerous, if not just sloppy, to use eval. It causes all sorts of performance and security issues.

You can simply do:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $key = 'hello';
  3. $value = 'johnny';
  4.  
  5. ${$key} = $value;
  6. ?>
  7.  
Nov 6 '08 #4
bilibytes
128 100+
ok thanks for clarifying.

could i do:

Expand|Select|Wrap|Line Numbers
  1. $this->{key($array)} = $array[key($array)];
?

the point is that i want to make a class that would handle all the HTTP requests of my application.

and i am trying to put the $_REQUEST array's content into variables of an instance of that class, without having to put to many if conditions.

is it good to do this?

i think it clarifies the code but maybe its not a good practice.

please tell me if what i want to do is totally stupid. maybe its better to just use $_REQUEST['key']...


as i saw that, in PHP manual, in the extract() function documentation, they talk about using extract to handle $_REQUEST array, i thought it would be great to make an object for that. but there is no option to extract() the array content into object variables" $this->variable". extract() only extracts to normal vairables "$variable". that is why, i was trying to figure out how to do that...

long story!

thankyou
Nov 7 '08 #5
Atli
5,058 Expert 4TB
So basically, all you are trying to do is;
Expand|Select|Wrap|Line Numbers
  1. // to change this
  2. echo $_REQUEST['name'];
  3.  
  4. // into this?
  5. $request = new RequestObject();
  6. echo $request->name;
  7.  
Why?

Unless you plan on having your class automatically sanitize or validate your fields?

I usually get request variables via a controller class that automatically sanitizes the data, but it doesn't need to be as complicated as you are trying to make it.
A simple static "getField($key)" method that gets and processes the field before returning it to you usually does fine.
Nov 7 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by pmclinn | last post: by
12 posts views Thread by Oliver Block | last post: by
1 post views Thread by dnn | last post: by
1 post views Thread by stef mientki | last post: by
8 posts views Thread by =?Utf-8?B?U2hhd24=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.