Echoing html

Hi, having a little problem echoing out some html. The line is

echo "<p>Thank you <b>$uname;</b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>"

How could I echo this out?

cheers

Just escape the quote characters with slashes:

echo "<p>Thank you <b>$uname;</b>, your information has been added to the database, you may now <a href=\\"main.php\\" title=\\"Login\\">log in</a>.</p>";

Alternatively, you can use single quotes in the HTML and double quotes to hold the string:

echo "<p>Thank you <b>$uname;</b>, your information has been added to the database, you may now <a href='main.php' title='Login'>log in</a>.</p>";

And don’t forget to end your lines with a semicolon.

in addition, the external quotes just need to be the opposite of the internal quotes so the parser can see where the actual string starts and where it ends.

 
echo [B][COLOR=red]'[/COLOR][/B]<p>Thank you <b>$uname;</b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>[COLOR=red][B]'[/B][/COLOR];

Kalon, That’s correct, but in this case the double quotes are needed on the outside as I suggested, because the poster is using a variable inside the string.

yep you’re right - good catch :slight_smile:

I missed the variable.

an alternative then is

 
echo '<p>Thank you <b>'.$uname.'</b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>';

which is the way I normally prefer to do it.

I have actually tried all three approaches, and your right about the double quotations.

However Zarin, both of your approaches only print out a semicolumn where the variable should be printed. I was thinking the variable might be empty, but upon testing it is not, and anyways I dont think its semicolumn would even be printed if it was empty?

p.s. In addition, would someone mind explaining to me when to use single and double quotation marks. My approach at the moment is problably bad, as its a case of using what I feel like at the time.

The semicolon inside the string looks unnecessary to me. It will be parsed as text and printed whether the variable is empty or not. Your variable should definitely be printing out though, so long as you are using double quotes for the string. If not, you can try several other methods:


String Concatenation:
echo "<p>Thank you <b>" . $uname . "</b>, your information has been added to the database, you may now <a href=\\"main.php\\" title=\\"Login\\">log in</a>.</p>";

Multiple args for echo:
echo "<p>Thank you <b>", $uname, "</b>, your information has been added to the database, you may now <a href=\\"main.php\\" title=\\"Login\\">log in</a>.</p>";

Variable encapsulation: 
echo "<p>Thank you <b>{$uname}</b>, your information has been added to the database, you may now <a href=\\"main.php\\" title=\\"Login\\">log in</a>.</p>"

I’m not 100% sure on the encapsulation, I’m reasonably sure it should work though. If you’re doing this with DB calls, try just setting $uname to some string value to test the echo statement first.

p.s. In addition, would someone mind explaining to me when to use single and double quotation marks.

there’s no definitive right or wrong way.

but any variables enclosed in double quotes will be evaluated first.

 
echo 'I am '.$age.' years old';

is the same as

 
echo "I am $age years old";

I prefer to use the 1st notation because my IDE then displays the variable name in a different colour to the string and so syntax errors like missing or miss-matched quotes are much easier to spot than if the string and variable name were all the same colour.

Generally the best way to echo that kind of content is using heredoc and the “Complex (curly) syntax”. E.g.


echo <<<OUTPUT
<p>Thank you <b>{$uname};</b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>
OUTPUT;

You don’t have to deal with the ugliness and hassle of string concatenation, echoing multiple arguments, or escaping quotes. Using the {$var} syntax inside strings clearly delineates where the variable interpolations are.

If you have a big block of HTML with no variables, or you need to make function calls amidst the HTML as well as interpolate variables, then this is sometimes a better solution:


<?php
...
?>

<p>Thank you <b><?php echo $uname; ?>;</b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>

<?php
...
?>

p.s. In addition, would someone mind explaining to me when to use single and double quotation marks. My approach at the moment is problably bad, as its a case of using what I feel like at the time.

You should check out the PHP documentation on strings if you haven’t already. Learning about them will probably help you choose the appropriate way to represent your strings. In general, use double quotes or heredoc if you need variable interpolation or escape sequences like
. If your string contains single quotes, then you can use double quotes or heredoc. If the string contains double quotes, then you can use single quotes or heredoc. You can also escape quotes in strings when it makes sense. There are a lot of nuances, so the appropriate choice will depend on the situation.

If he is finding it hard to understand variable encapsulation, I doubt if he would understand heredoc at all. My recommendation would be to use variable encapsulation :slight_smile: