Parsing $variable value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tokci
Forum Newbie
Posts: 5
Joined: Tue Sep 09, 2008 2:42 am

Parsing $variable value

Post by Tokci »

I've a problem which I'm not able to figure out. I'm fetching some values from the db, which store some variable name.

For example, results from the db will be :

Code: Select all

 
array('parameter_name'=>'id','parameter_value' => '$id')
 

In the page I'm retrieving the result there is already a variable named $id (with a some value, say 'Test').

Now my problem is that I need to echo the value of result['parameter_value'] and it should print 'Test', but currently its echoing $id.

I've used:

Code: Select all

 
echo result['parameter_value'];
echo "result['parameter_value']";
 
Kindly let me know if this is at all possible or is there a workaround.
Looking forward for some guidance...thnx.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Parsing $variable value

Post by onion2k »

You need to use a reference, also known as a "variable variable".

Instead of storing $id in the database just store the name of the variable, eg id. Then use $$result['parameter_value'] to access it. Note the two $ signs. PHP parses the first to get 'id' then parses the second to get the value of ${'id'}.

They're very powerful. They're also a very easy way to write code that's virtually impossible to debug. So be careful.
Tokci
Forum Newbie
Posts: 5
Joined: Tue Sep 09, 2008 2:42 am

Re: Parsing $variable value

Post by Tokci »

onion2k wrote:You need to use a reference, also known as a "variable variable".

Instead of storing $id in the database just store the name of the variable, eg id. Then use $$result['parameter_value'] to access it. Note the two $ signs. PHP parses the first to get 'id' then parses the second to get the value of ${'id'}.

They're very powerful. They're also a very easy way to write code that's virtually impossible to debug. So be careful.

Hey ...just figured it out few minutes back...I'm doing the exact same thing you mentioned....so I guess I'm on the right track.

Thnx a lot for your input, I really appreciate it.
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Parsing $variable value

Post by marcth »

There's an error in your code:

Code: Select all

 
array('parameter_name' => 'id','parameter_value' => $id)
 
If you wrap the $id variable in single quotes it becomes the string '$id' and not the value of $id
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Parsing $variable value

Post by onion2k »

marcth wrote:There's an error in your code:

Code: Select all

 
array('parameter_name' => 'id','parameter_value' => $id)
 
If you wrap the $id variable in single quotes it becomes the string '$id' and not the value of $id
If you'd read the post properly you'd know that's what he wanted.
Post Reply