Variables with a variable?

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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Variables with a variable?

Post by spacebiscuit »

Hi,

I am using variable to store html links:

$data="/home/$_SESSION[link]";

I am using the variable as follows:

Code: Select all

<?
echo"< a href=$data>Link here</a>";
?>
Is it possible to output the variable and the variable within the variable. At present with the abovce code it simple outputs:

"home/$_SESSION[link]"

Thanks in advance,

Rob.
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: Variables with a variable?

Post by emmbec »

Concatenate the variable $_SESSION[link] to your original variable:

Code: Select all

$data="/home/".$_SESSION[link];
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Variables with a variable?

Post by spacebiscuit »

That is no tpossible i nmy case as the variable data is stored in a mysql database.

The links are pulled from the database row by row and all vary. In this particular case the row/link is:

'/home/$_SESSION[link]'

Any ideas?

Thanks,

Rob.
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: Variables with a variable?

Post by emmbec »

Here is what I did and it works, just modify it to your needs.

Code: Select all

$_SESSION[link]="hello_world.php";
$data = "link/test/$_SESSION[link]";
echo"<a href=\"".$data."\">Link here</a>";
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Re: Variables with a variable?

Post by emmbec »

robburne wrote:Hi,

I am using variable to store html links:

$data="/home/$_SESSION[link]";

I am using the variable as follows:

Code: Select all

<?
echo"[b]< a[/b] href=$data>Link here</a>";
?>
Is it possible to output the variable and the variable within the variable. At present with the abovce code it simple outputs:

"home/$_SESSION[link]"

Thanks in advance,

Rob.
You have an extra space there, remove it too:

Code: Select all

echo"[b]<a[/b] href=$data>Link here</a>";
That should work.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Variables with a variable?

Post by spacebiscuit »

It is not quite that simple guys.

the variable $_SESSION[link] is written on the fly and not known in advance of the code execution. So in effect when link is read from the database the $_SESSION[link] part is simply a reference to some dynamic data.

Here is my exact code.

From the database the link row is:

$link_data="/home/$_SESSION[link]";

I am actually adding the link data to a html map - over a grpahic:

<area shape="rect" coords="<? print_r($cordinates)?>" href="<? echo"$linkdata ?> ">

It is almost as if within the link_data variable I need to exit the echo and print the $_SESSION[link] variable.

I hope I am making sense here.

Thanks,

Rob.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Variables with a variable?

Post by VladSun »

I don't think it's a good software solution - does every record in the DB contain the $_SESSION['link'] string? If, so - remove it and concatenate at execution time. If not - using eval() might be a solution, but I would advice you to reconsider your solution.
There are 10 types of people in this world, those who understand binary and those who don't
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Variables with a variable?

Post by spacebiscuit »

Thanks for your feedback guys.

Vladsun - the eval method works perfectly, why is it not a good solution?

In answer to your question, every record does not contain the session link.

Thanks,

Rob.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Variables with a variable?

Post by VladSun »

robburne wrote:Vladsun - the eval method works perfectly, why is it not a good solution?
Because eval() is security vulnerable by nature :)
I don't know where do you get contents for your DB rows and $_SESSION from and whether they could be injected with a malicious PHP code.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply