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

Max Hallinan
Max Hallinan
7,614 Points

Syntax for opening and closing PHP block

In the Working With Get Variables video of the Adding A Contact Form badge, Randy writes this code:

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

And further down the page, the if/else statement is closed with this line:

<?php } ?>

I'm interested by the use of <?php and ?> to separate the PHP from the HTML. Even though the if/else statement isn't complete at the end of the third line, the script itself is closed and then reopened further down the page simply to insert the final curly brace. What's happening here? What's the reason for doing this? What are the rules for doing this? Why not just echo out the HTML?

1 Answer

Kevin Korte
Kevin Korte
28,148 Points

Remember that the php is only read by the server, and the server can only read the php. So ignore all of the HTML stuff and you'll see what the server is reading.

It doesn't really care how many php blocks you open and close, as long as what is opened is closed before a new block is opened again, it's going to execute the just the php code.

You wouldn't want to echo out HTML because that can get very messy very quickly to echo html syntax in php. Especially if it's fairly complex html, it's just fair easier to close a loop, put in your html, and put your closing bracket back in at the end.

It might be confusing today, but it'll make much more sense later.

Essentially in your example you're asking the server if two conditions are meant, and if it's true, send whatever is the first block to the browser, if it's false than send the second block.

Did that make sense, I felt repetitive.

Max Hallinan
Max Hallinan
7,614 Points

Thanks. I understand the conditional statement and the concept of server-side languages. What I was wondering is why it is possible to break a single conditional statement into several pieces and distribute those pieces across the page, each within its own PHP block. I was under the impression that opening and closing a PHP block indicated a level of finality similar to script tags for JavaScript. But in JavaScript it wouldn't be possible to start a conditional statement in one script tag, close that tag halfway through the conditional statement, and then continue the conditional statement in a new script tag further down the page. You're saying the end of a PHP block is not synonymous with the end of a script. That's helpful and makes sense for interlacing HTML and PHP. Thanks for your help.

Kevin Korte
Kevin Korte
28,148 Points

As far as I'm aware, you can't do the same in Javascript because it is also read by the browser, so it (the browser) has to execute the code as it goes down the DOM, which would of course break it.

But yes, the end of the php script tag doesn't indicate it's the end of a block of code.