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 trialTaylor O'Reilly
8,651 PointsJumping in and out of PHP if/else
Maybe this is something I am just overlooking but around 2:30-2:40 we are dealing with the if/else based on that status of $_GET. All of this made sense up until the part I saw this:
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly.</p>
<?php } else { ?>
I don't really understand why the php tag is closing there after the { and after the else.
I am assuming this is because if we closed it where it makes sense to me, we would have to echo out the code for example
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {echo "<p>your awesome text</p>" ?>
and that just doesnt seem like a good preactice
can someone shed some light on this? Thanks!
2 Answers
Stone Preston
42,016 Pointsbecause if you kept everything inside one php block you would have to echo out that html tag. breaking the php block up into pieces allows you to write plain html that will appear on the screen without having to use the echo command to output it.
heres what you would have to do if you did it without breaking it up
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {
echo "<p>Thanks for the email! I’ll be in touch shortly.</p>";
} else
{ ?>
obviously its not that hard to do for the above case, but when you have to echo out multiple variable values or stuff with lots of quotation marks and include them in html its easier to break it up so you dont have to concatenate a bunch of stuff together. you can just break your php statements up into small pieces and then you dont have to deal with concatenating everything together into one big string.
here is a situation where it gets messy
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {
echo "<p>Thanks for the email," . $name . "!" . "I’ll be in touch by" . $date ." </p>";
} else
{ ?>
when you have multiple variables that need to be output, you have to concatenate them together and its very hard to read. you can separate your php tags like this to make it easy:
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email,<?php echo $name?>! I will be in touch by<?php echo $date?></p>
<?php } else
{ ?>
it doesnt just apply to if statements either, anytime you have to weave html in with your php you may want to break it up.
Taylor O'Reilly
8,651 PointsWhen I saw the php close it seemed like everything that comes after wont be in relation to the php anymore. With your explanation it seems like I am really just breaking up one tag into two separate parts which is a lot easier to wrap my head around. Thanks for the help!
Taylor O'Reilly
8,651 PointsTaylor O'Reilly
8,651 PointsThat part makes sense but in the series we never (that i remember) did this before and it was done without explanation. I can see how this is much better to do but I don't know where and when I should be doing it.
Stone Preston
42,016 PointsStone Preston
42,016 Pointsi added a bit more to my answer to show you when you might want to break it up. basically anytime you have to output multiple variables inside html is when you want to do that.
Taylor O'Reilly
8,651 PointsTaylor O'Reilly
8,651 PointsGo figure. Two videos after the one I was on is all about my exact question. Patience is a virtue ha. That was a really quick response though I appreciate it.