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

PHP Build a Simple PHP Application Adding a Contact Form Working with Get Variables

Arnold Rosario
Arnold Rosario
16,357 Points

Why is there a closing bracket in a separate PHP tag?

I don't understand why the conditional is coded like this:

<?php if ($_GET["status"] == "thanks") { ?>
    <p>Thanks for the email! I&rsquo;ll be in touch shortly.</p>
<?php } ?>

Why do you close the PHP tag after the opening bracket and then open another PHP tag on the closing bracket?

2 Answers

Everything within the PHP tags is evaluated as PHP. In this case the paragraph tag markup is not valid PHP and just something we want to print to the document if the condition is met. To avoid having to use echo or other method of printing to the document via PHP which can get messy we can simply close the current PHP block, write the markup we want to use, and then open a new block to finish the conditional. You can use this technique to print repeated content using loops as well for example.

Arnold Rosario
Arnold Rosario
16,357 Points

Thank you! I'm not sure if this was explained in the video? Maybe I missed it, but thank you, very clear!

Andy Zervas
Andy Zervas
3,386 Points

I head the same question and I tried to write it with echo instead, but couldn't make it work. If anyone has done it, I'd like to see it.

You should be able to just echo the entire paragraph tag within the conditional. If you needed to insert variables into the output you'd have to use string concatenation.

<?php
  if ($_GET["status"] == "thanks") {
    echo "<p>Thanks for the email! I&rsquo;ll be in touch shortly.</p>";
  }
?>