Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Martin Hodgson
Courses Plus Student 1,582 PointsUsing a conditional to display a form or thank you message.
I'm working through the PHP contact form tutorials and I am confused when I get to the "working with Get variable" stage.
I don't understand the location of the <?php and ?> in the 'if' and 'else' blocks.
Why does the code within the { } start with '?>', and end with '<?php' ?
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
<p>I’d love to hear from you! Complete the form to send me an email.</p>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
4 Answers

Mike Costa
Courses Plus Student 26,362 PointsHey Martin,
The reason for this is to separate the php code and html code. PHP wouldn't know what to do with a <p> tag just as html wouldn't know what to do with an if/else statement. By closing the php tags before your html code, you separate the 2 languages instead of embedding html inside php code. Here's an example of what I mean by embedding the two languages:
<?php
if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {
//using echo to output html to the browser
echo "<p>Thanks for the email! I'll be in touch shortly!</p>";
} else {
echo "<p>I'd love to hear from you! Complete the form to send me an email.</p>";
echo '<form method="post" action="contact.php">';
...
...
}
I've always thought this to be bad practice to embed html within php code. It could also lead to unexpected errors in php scripts. Separating php and html leaves the code a lot cleaner and easier to read.
Hope this helps!

Martin Hodgson
Courses Plus Student 1,582 PointsHi Mike, thanks for getting back to me.
So the first '?>' within the 'if' statement closes the php, separating it from the paragraph, but then reopens the php so the 'else' statement can read. Then the 'else' statement closes it again to separate it from the html form ?
Am I on the right lines here?!
I'm still not sure what the last '<?php' is for below the form code though?
I can move past this stage but, I really want to fully understand what I have learned before I do.

Randy Hoyt
Treehouse Guest TeacherHey Martin,
This is one thing I didn't cover as well as I would have liked in the first project. (I'm actually recording a video explaining this better today!) I answered this question for someone in a previous thread. Take a look at that thread and my response:
- Treehouse Forum: Using Multiple PHP Strings
Does that help?

Martin Hodgson
Courses Plus Student 1,582 PointsHi Randy Yes, I understand it now. I knew that HTML inside php code wouldn't be rendered, I just didn't quite follow the structure of the opening and closing of the php brackets.
Thanks !