Page 1 of 1

displaying a variable in an echo statement

Posted: Thu Sep 11, 2003 1:50 pm
by chriso
I have written the following code:

function addfaccert($fid, $flname) {
$cert_array = @mysql_query('SELECT * FROM certs ORDER BY CERTS');
if (!$cert_array) {
die('<h2>Error retrieving certs from table!</h2><br>' .
'Error: ' . mysql_error());
}
echo ('
<p align="center">Add Certification for $flname<p>
<table align="center" border="0" width="70%" cellspacing="6">
');

I have tried every combination of "$flname", \"$flname\", '$flname', ''$flname'', etc to get the variable $flname to display its value but all I get is the printing of the actual variable, i.e. the webpage will display "$flname", or \"$flname\", etc. How do I get the value to print to the browser? (I've left off the rest of the function for space consideration since it doesn't deal with the question)

Thanks.

Posted: Thu Sep 11, 2003 1:54 pm
by phice
When you're including a variable inside an echo statement, do: {$variable} instead of just $variable

Posted: Thu Sep 11, 2003 2:12 pm
by m3rajk
phice wrote:When you're including a variable inside an echo statement, do: {$variable} instead of just $variable
that's only needed if it's an array.
otherwise "blah blah $variable balah blah" works fine

Re: displaying a variable in an echo statement

Posted: Thu Sep 11, 2003 2:30 pm
by Sinnix
chriso wrote:echo ('
<p align="center">Add Certification for $flname<p>
<table align="center" border="0" width="70%" cellspacing="6">
');

Code: Select all

echo ("<p align='center'>Add certification for ".$flname."<p>
<table align='center' border='0' width='70%' cellspacing='6'>");

Re: displaying a variable in an echo statement

Posted: Thu Sep 11, 2003 2:55 pm
by JAM
Sinnix wrote:

Code: Select all

echo ("<p align='center'>Add certification for ".$flname."<p>
<table align='center' border='0' width='70%' cellspacing='6'>");
Last example, but using "" around the html-values (html good):

Code: Select all

echo ('<p align="center">Add certification for '.$flname.'<p>
<table align="center" border="0" width="70%" cellspacing="6">');

Posted: Thu Sep 11, 2003 4:14 pm
by chriso
Thanks everyone for you quick responses. I love php and I'm happy to learn better scripting methods.

Posted: Thu Sep 11, 2003 7:56 pm
by phice
" . $variable . " is the same as {$variable}, so I go with what's less confusing and faster to type. ;)

Posted: Fri Sep 12, 2003 2:38 am
by twigletmac
Just as a note, the actual problem here was that PHP does not parse strings within single quotes so:

Code: Select all

$info = 'foobar';
echo 'blah $info blah';
will produce

Code: Select all

blah $info blah
whereas

Code: Select all

$info = 'foobar';
echo 'blah '.$info.' blah'; // my preferred method
// or 
echo "blah $info blah"; // note the double quotes
will produce

Code: Select all

blah foobar blah
Mac