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
ryanandrews
7,822 PointsNeed some clarification workinig with the code in "Working with Get Variables", Lost in the semantics of it.
In the lesson "Working with Get Variables", from the Contact From section of Build a simple PHP application there is a if/else statement added to know which contact page to display. I follow the theory of it but being new I am lost in some of the semantics of the code.
Where I am confused is, I know that <?php.....?> is what starts and ends a php code but in this exampe the curly brackets of the if/else statement are in and out of these. I have looked at it for an hour now trying to figure it all out and make sense of it, but I can't.
Please help me explain so that I am starting with a solid foundation.
Example: at the bottom of the code is <?php }?> ( I don't get this) or the <?php } else { ?> found at the top.
This is my first forum question so sorry if it is unclear.
Thanks
<?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="email" 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
Randy Hoyt
Treehouse Guest TeacherHey Ryan,
Good question! I talk about it a bit more in depth in a future video, Mingling HTML and PHP; sorry for the confusion here.
The code inside the PHP tags is treated as PHP code that gets processed by the server. The code outside the PHP tags is treated as HTML that gets sent down to the browser. You can do things like this:
<?php
$name = "Randy";
?>
<h1>My name is <?php echo $name; ?></h1>
We need the two sets of PHP tags because we have two separate blocks of PHP code mingled inside our regular HTML tags.
Take this code for example, with curly brackets around the code inside the foreach loop:
<?php
if ($flavor == "cookie dough") {
echo $message;
}
?>
If we want to wrap HTML tags around this message, we can end the PHP code after the first curly bracket, display some regular HTML, add some more PHP code to output the variable value, display some other regular HTML, and then add some more PHP code to close the foreach loop:
<?php
if ($flavor == "cookie dough") {
?>
<p><?php echo $message; ?></p>
<?php
}
?>
I know it's a little strange that a conditional can span multiple blocks of PHP code, but this is how you mingle PHP and HTML together.
Does that help?
Edmond Egan
2,523 PointsThe <? } ?> at the bottom just closes the if function
Remember <?php and ?> are not start and end of a conditional/function, as you can open and close blocks of code in lots of places still within the same block of code.
<?php <<- php opened
if ($value = "Test") { ?> <<--php closed
<HTML CODE STARTS HERE> <<-- we can write HTML code here as we are outside of PHP
<?php } ?> <<- php is opened, if conditional closed with the } and php closed.
This is the way i understand it, does this help ?
Edit: updated to clarify conditional / function, also yes, i didn't comment it properly but nvm.
Ed
Pavol Almasi
Courses Plus Student 1,524 PointsIn PHP if a condition is true/false, you do not have to do another line of PHP - you can output HTML instead. However, before you do that, you need to open the conditional bracket "{", and close PHP. All the HTML within the { } is simply what happens (what is output) if the condition is true/false. You could simply echo the whole HTML if you wanted to and never escape from the PHP at all.
ryanandrews
7,822 Pointsryanandrews
7,822 PointsThank you everyone for your help. Randy thanks for explaining it here and in your later video, I have completed watching your video on Mingling HTML and PHP and the concept is much clearer to me now. Thank you again for the help and I am enjoying your tutorial videos very much, thanks.