displaying a variable in an echo statement

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
chriso
Forum Commoner
Posts: 31
Joined: Fri Aug 01, 2003 11:52 am

displaying a variable in an echo statement

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

When you're including a variable inside an echo statement, do: {$variable} instead of just $variable
Image Image
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Re: displaying a variable in an echo statement

Post 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'>");
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: displaying a variable in an echo statement

Post 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">');
chriso
Forum Commoner
Posts: 31
Joined: Fri Aug 01, 2003 11:52 am

Post by chriso »

Thanks everyone for you quick responses. I love php and I'm happy to learn better scripting methods.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

" . $variable . " is the same as {$variable}, so I go with what's less confusing and faster to type. ;)
Image Image
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply