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

William LaMorie
William LaMorie
11,769 Points

What with the syntax on handling the get?

Ok, I've got a couple of questions here...

  1. What's the deal with writing this:
      <?php if ($_GET["status"] == "thanks") { ?>
        <p>Thanks for the email! I&rsquo;ll be in touch shortly!</p>
      <?php} ?>

when this looks a lot better, is a lot less confusing, and does the same thing as far as I can tell:

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

and :

  1. How does the 1st method even work?!? I don't get how the <p> isn't just sent to the client, it looks like:
<?php script ?>
  html
<?php script ?>

to me... can someone explain?

Thanks, Will

3 Answers

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

To expand on what Logan said...the reason you don't always see the html output to the browser is because it's wrapped inside the { and } of your php conditional. Whatever code is written between those curly brackets (whether it's php, as in your second example, or html, as in your first example), only gets executed when your if statement is true. If it's false, it skips that whole block of code.

You're right that the echo function works perfectly well here, but in production it's not always as practical. For example, instead of a single line of simple text, you might have already coded a whole table of data...or a series of nested divs with classes...and a bunch of text with hyperlinks that you want to output. In this case, echoing out your html becomes more problematic. You'll probably have to edit your html to make sure you don't have a mix of single and double quotes (as this could interfere with the execution of your php), or in some cases, you might even have to have a series of a few dozen echo statements...which becomes really tedious to code, and difficult to edit.

With this method:

<?php if($this == 'True') { ?>
Your html code here
<?php } ?>

you'll basically be able to copy and paste your pre-coded html into place, without having to worry about re-formatting for the echo function. And if things change later on, it will be much easier to edit.

Hope this makes sense.

Old Times
Old Times
9,301 Points

Thank you so much, it made a lot of sense to me :)

Logan R
Logan R
22,989 Points

In PHP, you start your code by doing <?PHP and end it by doing ?>. You are allowed to start and end your PHP code as much as you want.

Both methods work and which one you want to do is totally up to you. It more comes down to style and how you think which one will be more readable to you or future people who work on your code.

Edit: The <p> is not shown in plain text when echoed because the PHP code is rendered on the server first and is then sent to the browser as HTML.

William LaMorie
William LaMorie
11,769 Points

That makes sense. It's a bit difficult to wrap your head around the whole relationship between the php processing on the server and the html document, especially with many layers of embedding. I keep seeing the <?php ?> script calls as closed spaces, and I keep trying to wrap my head around what's going on with the namespace here and there.

Tonny Ouma
Tonny Ouma
3,661 Points

Thanks William for bringing this up I couldn't wrap my head around this either but at the same time it was getting tedious on writing echo statement each time I needed htmls