Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

James Barrett
James Barrett
13,253 Points

Need help with echoing HTML!

Hi there,

I am trying to echo out some HTML however I am struggling to understand where single and double quotes should go. I have tried these so far and the webpage still crashes every time:

echo "<p>" "Date: " . $row["P_Dateadded"] . " Content: " . $row["P_Body"] "</p>";
echo "<p> 'Date: ' . $row['P_Dateadded'] . ' Content: ' . $row['P_Body'] </p>";

This may be completely wrong, I am struggling to get to grips with what should be single and what should be double quotes at the moment!

Thanks, James.

2 Answers

Henrik Hansen
Henrik Hansen
23,176 Points

When you are creating a string with lots of variables you should use the " ".

<?

//Fairly easy to read:
$dateAdded = $row["P_Dateadded"]
$pBody = row["P_Body"]

echo "<p>Date: $dateAdded Content: $pBody,</p><br>";

// Single and double quotes are pretty much the same:
echo "<p>Date:" . $dateAdded . 'Content: ' . $pBody . ",</p><br>";

?>

The real difference between them are that in " " $variables will expand, and you can use \n\r for new line.

But using " " in output strings and ' ' in short strings (like for $array['index'] ) is probably pretty common.

Edit:

I saw this line in your question too:

<?

// If your are within one kind of quote, you will have to end it with the same type of quote. This is wrong:
echo "<p> 'Date: ' . $row['P_Dateadded'] . ' Content: ' . $row['P_Body'] </p>";

// This will be a string that reads: I'm into php.
$string = "I'm into php.";

// This will be: She said "Wait!", and I stopped.
$string = 'She said "Wait!", and I stopped.';

?>
Andrew Shook
Andrew Shook
31,709 Points

Best I can tell you are making some mistakes with your concatenation.

your code:

<?php
echo "<p>" "Date: " . $row["P_Dateadded"] . " Content: " . $row["P_Body"] "</p>";

fixed code"

<?php
echo "<p> Date: " . $row["P_Dateadded"] . " Content: " . $row["P_Body"] . "</p>";

Basically you forgot to add "." period between the first p tag and the string "Date" and then forgot one between $row["P_Body"] and the last p tag.