Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Maximiliane Quel
PLUS
Maximiliane Quel
Courses Plus Student 55,489 Points

working with get variables: php code syntax

I'm trying to understand why the PHP tags are wrapped around the code the way they are in this tutorial.

So why can't I just write:

<?php
if (isset($_GET["status"]) AND $_GET["status"] == "thanks"] == "thanks") {
    //do something
} else {
    //do the other
}
?>

but have to use this syntax instead?:

<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks"] == "thanks") { ?>
    //do something
<?php } else { ?>
    //do the other
<?php } ?>

I'd really appreciate if somebody could explain, because I wouldn't have come to the second version myself.

Thanks!

3 Answers

Both ways could be made to do the same thing. The reason the second way is being used in the lessons is probably because the stuff where your //do something comment is is HTML code, rather than PHP. Since it's HTML, you want it to be sent to the browser as-is, and you don't want it to be treated as PHP. If you used the first method that you have, you'd have to print the HTML with an echo command, rather than just putting the HTML directly into the body of your if block. Both ways would work though.

The following two blocks of code would do the same thing.

<?php 
if (isset($_GET["status"])) {
   echo '<p>Hello</p>';
} 
?>

or

<?php if (isset($_GET["status"])) { ?>
   <p>Hello</p>
<?php } ?>
Stone Preston
Stone Preston
42,016 Points

because you cant put any html inside the php unless you echo it out. in other words with this way

<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks"] == "thanks") { ?>
    <h1>blah</h1>

<?php } else { ?>
    <h2>blah</h2>
<?php } ?>

see how you can put html between the control flow statements? As opposed to this way

<?php
if (isset($_GET["status"]) AND $_GET["status"] == "thanks"] == "thanks") {
    echo "<h2>blah</h2>";
} else {
   //this wont work
   <h2>blah</h2>

}
?>

Having an opening and closing php tag before and after each line in the control block lets you dynamically display the html easily. You could display the html with an echo statement, but that can get cumbersome.

Maximiliane Quel
PLUS
Maximiliane Quel
Courses Plus Student 55,489 Points

Thanks guys! That's really helpful. Seems so simple when you see the explanation. Not sure what to do when both answers are the best ;0)