Forum Moderators: coopster

Message Too Old, No Replies

PHP 101 - Displaying a variable within a variable

Is this possible - or is there a better way?

         

Spook

11:24 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



I have a php site template that has been working quite well, but I decided to make it better - and broke it!

I am using a single index.php file which "includes" header, footer, and sidebar information. The page content is also included and changes dependant upon which nav link is clicked.

Content is mostly html and is stored in the $content variable and is echoed to the page.

However, I have an html form which displays correctly, but the form processing is done using php. Resulting messages are assigned to variables and "should" be displayed accordingly. But I just can't get the thing to work correctly.

Below is a section of my code. $legend and $errmsg are set by the form_functions.php file which is included before the $content variable [removed to reduce code posting]. The table displays ok, but not the $legend or $errmsg

Must be something simple but I've been trying to solve this for about 4 hours - problem is, I only take care of my own sites so don't get too much practice - any advice much appreciated.

TIA - Spook

<?php

$content = '

<table width="100%" height="600" border="1" bgcolor="#CCCCCC" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<fieldset>
<legend><?php echo $legend;?></legend>

<?php echo $errmsg;?>

</fieldset></td>
</tr>
</table>

';?>

panos

2:30 am on Oct 28, 2005 (gmt 0)

10+ Year Member



<?php

Try this:

$content = "<table width=\"100%\" height=\"600\" border=\"1\" bgcolor=\"#CCCCCC\" cellspacing=\"0\" cellpadding=\"0\"><tr><td valign=\"top\">
<fieldset><legend>".$legend."</legend>".$errmsg."</fieldset></td>
</tr></table>";?>

panos

3:08 am on Oct 28, 2005 (gmt 0)

10+ Year Member



oops!

<?php
$content = "<table width=\"100%\" height=\"600\" border=\"1\" bgcolor=\"#CCCCCC\" cellspacing=\"0\" cellpadding=\"0\"><tr><td valign=\"top\">
<fieldset><legend>".$legend."</legend>".$errmsg."</fieldset></td>
</tr></table>";
?>

sneaks

4:40 am on Oct 28, 2005 (gmt 0)

10+ Year Member



variable variables are simple, just use $$

example:

$vString = "var_" . "hello";

$$vString = "hello";

in other words, now we have created a variable, $var_hello = "hello"

goodluck

Spook

11:54 am on Oct 28, 2005 (gmt 0)

10+ Year Member



Many thanks for your help.

The form now functions correctly using panos's code, but if I could impose on you a little further. Can you point me to where i can find out about the full stop before and after the variable as in: >".$legend."</ I have searched both the forum and php.net but can only find information about "Hyperwave Functions" which doesn't seem appropriate.

The $$variables looks interesting, but will take a little longer to get my head around.

Thanks again.

jatar_k

3:33 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it is the concatenation operator
[php.net...]

Spook

7:44 pm on Oct 28, 2005 (gmt 0)

10+ Year Member



Of course it is!

Sorry, my brain must have been in neutral.

Thanks again to all.