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 trialStephen Twomey
10,869 PointsWhy is the PHP Code written to have opening and closing brackets on multiple lines?
In "Working with Get Variables in the PHP Development Track, Randy writes the code in the following way:
<?php if(isset($_GET["status"]) AND $_GET["status"] == "thanks"){ ?>
<p>Thanks for the email! I’ll be in touch shortly.</p>
<?php }else{ ?>
<p>I’d love to hear from you! Complete the form to send me an email.</p>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
Why are the opening and closing brackets written on different lines like the following:
<?php if(isset($_GET["status"]) AND $_GET["status"] == "thanks"){ ?>
<p>Thanks for the email! I’ll be in touch shortly.</p>
<?php }else{ ?>
Wouldn't it be easier just to write one opening "php" bracket, and then at the very end of the long block of code one closing "php" bracket.
Is this just convention?
4 Answers
Lucas Santos
19,315 PointsReason why he writes it that way is because of the way he writes his PHP code is as if it was all in one php<?php ?>
bracket. What I mean by that is if you write ALL php code like this:
<?php
$name = "Mike the frog";
if(conditional){
echo $name . "is my name";
}else{
echo "Sorry no name.";
}
?>
you will notice how the brackets did not have any HTML in between it's pure PHP code. But now if you have html in between like you want to output a paragraph tag of some sort you can do it 2 ways.
First way is Randy's way of breaking the brackets and continuing his PHP code like so:
<?php
$name = "Mike the frog";
if(conditional){ ?>
<p>The conditional was true!</p>
<?php }else{ ?>
<p>Sorry the condition was not true</p>
<?php } ?>
Notice how I broke the brackets to continue my PHP code while inserting HTML if the conditional of the if statement was true.
Now the second way which I prefer better myself:
<?php $name = "Mike the frog" ?>
<?php if(conditional): ?>
<p>The conditional was true!</p>
<?php else: ?>
<p>Sorry the condition was not true</p>
<?php endif; ?>
They both are the same thing but notice how after the if statement I used : that is the symbol that says that the statement is not done and it will continue. As where ; means you are done with the statement and that line of code.
So really he's just continuing his PHP code in a different way which is the more common way when you want to use HTML in between you PHP code. Hope this makes some sense to you and gives you a better understanding.
Riley Hilliard
Courses Plus Student 17,771 PointsThink of it more like the PHP blocks as being a snippet of PHP logic, and everything in-between being literal HTML. EX:
<?php
// everything in here is PHP and will be read and evaluated by the server.
if($something == true){
// this is the beginning of a conditional, that means that if $something evaluates to true, the block will be included.
?>
<!-- This is literal HTML on the page -->
<div>
The HTML here will only be printed to the page if the '$something' var evaluates to true, other wise the PHP block that this HTML is inside will not be printed.
</div>
<?php
} // <--- that bracket is the closing bracket of the PHP 'if($something == true){ ' statement above.
?>
Amongst various reasons, this way can be more readable when you are mixing html and PHP. You are technically correct that this can all be in the same PHP tag, but you would need to use 'echo' to print out the HTML. That would look like this:
<?php
if($something == true){
echo "<div>The HTML here will only be printed to the page if the '$something' var evaluates to true, other wise the PHP block that this HTML is inside will not be printed.</div>";
}
Stephen Twomey
10,869 PointsOkay, so anything that would be evaluated by the server, that being the "pure" php code, is placed between the php tags. Anything else can be written outside of the php tags, but is still executed server-side, as needed.
Did I understand that correctly?
Thanks for the answers.
Lucas Santos
19,315 PointsYes anything that is not PHP will have to be written outside of the PHP tag so you have to break the tag, write your html them continue the tag like the examples.
Stephen Twomey
10,869 PointsOkay, great. Thank you.