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

Question on PHP brackets (Build a Simple PHP Application, Adding a Contact Form, Stage 3)

I don't understand the placement of the php tags in the video (the relevant snippet of code is written below). A closing php tag (is this the correct name?) is placed right after a left curly bracket. And then on the third line, a beginning php tag is inserted right before the right curly bracket!? So a closing and opening php tag are placed in the middle of the statement!

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

Below I have written the code as an abstraction of the particular example:

<?php if ( ... ) { ?> ... <?php } else { ?> ... <?php } ?>

1 Answer

David Omar
David Omar
5,676 Points

What you are doing here is coming out of php mode. after the first bracket of the if statement you are breaking out of php mode and outputting html. Now you can't leave it like this because it's not valid syntax, you need to close all braces. So you have to pop back into php mode to close the brace.

<?php 
if(true) { 
 ?> <p> Hello World, now we need to open php tags again to complete our if statement </p>
<?php } ?>

If your php file contains no html than you can omit the closing ?> php tag

Thanks, David. Seems obvious now. I think it's time to take a break.

David Omar
David Omar
5,676 Points

No problem, good luck with PHP!.