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
drewbumpas
5,529 PointsConfused by code structure
I'm not completely understanding the following code listed below. My confusion starts at the end of the first line. Why does the "{" symbol come before the closing PHP tag "?>" ? The PHP tags also enclose the "}" symbol on the third line which I also don't understand.
When I look at the first line, I would think that the curly brace symbol would start outside of the closing PHP tag.
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly.</p>
<?php } else { ?>
I hope this makes sense. The code can be found in contact.php from the Build a Simple PHP Application lesson.
4 Answers
dhillonsh
4,745 PointsIf this example were to be made only with PHP, no HTML, it would look like this:
<?php
if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {
echo "<p>Thanks for the email! I’ll be in touch shortly.</p>"
} else {}
?>
What this is saying is if $_GET['status'] equals 'thanks', then show the person "Thanks for the email...". This is why there is the open curly bracket before the message and a closing one after.
drewbumpas
5,529 PointsThank you! Now I understand. Are both ways acceptable to write or is one method preferred over the other? The way your wrote it makes much more sense to me structurally.
dhillonsh
4,745 PointsNo problem. I believe TreeHouse may force you to write it the way you had it, although I'm not quite sure. I personally dislike doing it the way they have it setup, just makes matters more confusing.
Giovanni CELESTE
20,961 PointsI may be wrong, but I think the reason for mixing PHP and HTML is the semantic of the code. HTML is used only for the view when PHP is used only for the controller (hope it doesn't confuse you). But at first (and still now), I think it's more intuitive to wrap it all in PHP (and more readable).
But as long as it works well, you can do as you want. They recommend to mix them, it's a more strict approach, probably best used for large projects with different technologies (switch PHP with Ruby, without touching HTML, you see).
I'm a beginner, just my comprehension of the courses. Hope it makes sense. Sorry for my english, french guy :)