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 trialEmy Ng
8,035 PointsWhy use <?php ?> in between the codes so many times, why not used it only one time?
<h1>Contact</h1>
<?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 an email.</p>
<form method="post">
<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 } ?>
3 Answers
Ash Anderson
3,184 PointsThis allows you to switch back and forth between HTML and PHP easily. If you didn't keep dropping in and out of PHP (<?php ?>) then you'd have to start echoing out all of those pieces of HTML like so:
echo '<table>';
echo '<tr>';
echo '<th>';
Sure, you could group all of that together in one echo to make it easier to write, but for edibility at a later date I'd definitely recommend like you have it above. Drop in and out of PHP as necessary.
Chris Southam
1,823 PointsInterestingly you've actually asked a very important question. Once you've completely your initial PHP learning and continue, you'll most likely switch across to using a templating language which will strip out all your PHP code from your HTML (known as a 'view') and replace it with something like, depending on the template language, {{$message}}.
James Andrews
7,245 PointsWhile Ash Anderson is generally correct let me expand on the answer.
echo "something";
is shorthand for
s short for
echo("something");
echo is a function and every time you call a function it costs you system resources. Either CPU time or Memory or both. This makes your PHP run very slowly.
<?php if($something): ?>
i want to say something here
<?php endif; ?>
is less resource intensive than doing this.
<?php if($something):
echo "i want to say something here"; endif; ?>
It is also valuable because if you don't use a template system like twig or smarty it allows a non php programmer (ie a front end HTML programmer) to go in and change HTML code without having to worry about breaking php syntax.
James Andrews
7,245 Pointsignore me fixed it up there.
Emy Ng
8,035 PointsEmy Ng
8,035 PointsFor more info there is a video after this stage that explains it http://teamtreehouse.com/library/build-a-simple-php-application/listing-inventory-items/mingling-php-and-html