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
S. K.
Courses Plus Student 2,497 PointsStuck: Working With Get Variables
In this video Randy explains how we can arrange our form to work in one page using conditionals, etc..
He instructs, and his contact.php looks like this at its' beginning, I'm pasting all of it so we can identify the problem clearly:
<?php
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "E-mail: " . $email . "\n";
// TODO: Send Email
header("Location: contact.php?status=thanks");
exit;
}
?><?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>
<div class="section page">
<div class="wrapper">
<h1>Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly.</p> } else { ?>
<p>I’d love to hear from you! Complete the form to send me and email.</p>
<form method="post" action="contact.php">
and the end of it looks like this:
<input type="submit" value="send">
</form>
}
</div>
</div>
<?php include('inc/footer.php'); ?>
It's frustrating because I couldn't see the bottom of his screen because of those pesky control won't disappear.
Now, I get an error whenever visiting contact.php that says:
Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\contact.php on line 84 (which is my last line)
The main thing I didn't understand, and too bad Randy didn't explain it, is why all those ?> are added before and after the if and else blocks and what tags are they supposed to close or open.
Help?
13 Answers
S. K.
Courses Plus Student 2,497 PointsUpdate 1:
Ok, got it to work after maximizing the screen and figuring that I missed a ?php before ending the curly brackets of the else statement, and a "?>" before the rest of the code.
I'm still wondering what these <?php and ?> are within this if and else statements, I'm trying to figure it out but it'd be nice if someone who is more literal with code could explain to me. Which tag is closing which opening one?
For example, which opening tag is this closing tag being applied to?:
AND $_GET["status"] == "thanks") { ?>
Update 2:
I now figured why these ending tags were put there, to close the if, same for the ?> right after the opening the curly braces of the else block. But why is it written that way?
What is the necessity of the additional PHP tags in the and inbetween the if and else blocks?
S. K.
Courses Plus Student 2,497 PointsUpdate #3:
I'm specifically not understanding why we open a <?php tag just before we close the curly braces of the if statement, since no PHP is included (or at least executed) afterwards?
J.T. Gralka
20,126 PointsS K,
Generally you can think of the <?php ?> tag as a self-closing tag, although it's more of a encapsulator for PHP code. It looks as if you're trying to do something like this:
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Hello</p>
<?php } ?>
You're writing an if statement that evaluates whether or not the value of status is set, and whether it's value is "thanks". If it is, the second line will be executed; in the case of the example code above, PHP will tell your browser to display a paragraph. The only thing you have to keep in mind is that when you're writing an if statement, the body of your code block has to be encapsulated by curly braces. Because of this, it's necessary to include the third line (which you've seemed to omit in your example above) that closes the block.
To make the above example more clear, I've rewritten the script so that the value is echoed by PHP rather than simply displayed as HTML.
<?php
if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {
echo "<p>hello</p>";
}
?>
Notice that my if block still ends with a closing curly brace.
Does that help answer your question?
Cheers,
J.T.
J.T. Gralka
20,126 PointsTo clarify, specifically to help address the question you bring up in Update #3, you should realize the difference between the two code examples I provide above.
In some of the tutorials, Randy demonstrates how to have HTML displayed conditionally. He doesn't echo the markup, he just has the HTML display. So, in other words, if the if condition were false, the HTML would not be sent to and therefore not rendered by the browser.
Because he breaks his PHP script down into HTML tags and PHP blocks, it's necessary to fill in some parts of the script so that his PHP is syntactically correct and so that his HTML is still semantically sound.
Does that make any sense or help to explain why you might have to include <?php } ?> after the HTML?
Randy Hoyt
Treehouse Guest TeacherHey S.K.,
I'm working on a revision to that video to explain that a little more fully; I have had some feedback that this isn't clear. Here's a link to a similar thread where I explain this:
Does that explanation help?
S. K.
Courses Plus Student 2,497 PointsRandy, thanks for addressing this promptly. While the link does a decent job of explaining the concept, I feel my grasp is a bit shaky looking at our code so far in Mike's website. I will try going over it a few times everyday so I get its' merits and importance.
S. K.
Courses Plus Student 2,497 PointsWell, I'm stuck again with grasping this concept, this time with the challenge, let me explain:
<?php foreach($products as $product) { ?>
<li><?php echo $product; ?></li>
<?php } ?>
I've copied our foreach and echo commands from the videos, just to explain what I have a hard time grasping: I understand the first two parts, we want to close the first set of php code so our li isn't treated as PHP code. Then, inside the li tags, we're opening a php echo command and closing it within the tags. But why are we adding the last <?php and ?> at the end?
why does our code look like the above and not like this structure:
<?php foreach($flavors as $flavor) { ?>
<li><?php echo $falvors; ?></li>
}
Randy Hoyt
Treehouse Guest TeacherHey S.K,
The last curly bracket is part of PHP. Let's start with this, a plain foreach loop in PHP:
<?php
foreach($products as $product) {
echo $product;
}
?>
That all makes sense, right? Everything in between the <?php and the ?> is treated as PHP code.
Now watch this. Right in the middle of the foreach loop, I can use a ?> to end the PHP code.
<?php
foreach($products as $product) { ?>
Now that there's a PHP closing tag has appeared, everything after it will be treated as plain old HTML:
<?php
foreach($products as $product) { ?>Product Name:
That last bit, "Product Name," is now just regular old HTML and not PHP code. If we want to start writing PHP code again, we'll need another opening PHP tag:
<?php
foreach($products as $product) { ?>Product Name: <?php
echo $product;
}
?>
Good so far?
Let's change "Product Name" to an HTML element, like this:
<?php
foreach($products as $product) { ?><li><?php
echo $product;
}
?>
Still good, right?
Now let's add some HTML after echoing out the product variable.
<?php
foreach($products as $product) { ?><li><?php
echo $product; ?></li>
I think you said you made it this far, right?
Now, after this, we need to start writing PHP code again. Why? The foreach loop needs an opening and a closing curly bracket:
foreach() {
....
}
Without a closing curly bracket treated as PHP code, the page will error. To get back to PHP code, we need third pair of PHP tags.
<?php
foreach($products as $product) { ?><li><?php
echo $product; ?></li><?php
}
?>
We can move all the hard returns around a little bit to make it a little cleaner, but this is the same exact thing as this:
<?php foreach($products as $product) { ?>
<li><?php echo $product; ?></li>
<?php } ?>
Does that help?
S. K.
Courses Plus Student 2,497 PointsWow, so the last set of <?php and ?> is only so the ending curly brace gets treated as PHP?
Randy Hoyt
Treehouse Guest TeacherExactly. Otherwise it would get treated as plain old HTML. But it's really PHP code that marks the end of the foreach loop.
Randy Hoyt
Treehouse Guest TeacherHere's one more way to think about it. Let's say you have a paragraph in HTML.
<p>Paragraph Paragraph</p>
Let's say you now want to break this into two paragraphs with a heading in between, so that it looks like this:
<p>Paragraph</p>
<h1>Heading</h1>
<p>Paragraph</p>
What you have essentially done is added this </p><h1>Heading</h1><p> in the middle of that. That's a closing paragraph, a heading, and an opening paragraph tag.
Now think of a block of PHP code in the same way. We start off with one block of PHP code:
<?php
foreach($products as $product) {
echo $product;
}
?>
We now want to break it into two blocks of PHP code with some HTML in between. What we need to add is a closing PHP tag, the HTML, and an opening PHP tag, like this: ?>HTML<?php.
The first place we'll add that is in between the start of the foreach loop (line 1) and the echo product (line 2):
<?php
foreach($products as $product) {
?><li><?php echo $product;
}
?>
The second place we want to do this is in between the echo product (line 2) and the end of the foreach loop (line 3). Once again, we'll add a closing PHP tag, the HTML, and then a new opening PHP tag:
<?php
foreach($products as $product) {
?><li><?php echo $product; ?></li><?php
}
?>
What do you think? Does that help?
S. K.
Courses Plus Student 2,497 PointsActually the first explanation was already more than sufficient, I just finished the challenge without referencing to the videos or my notes for the first time. Thank you!!!
When is the next series coming up?
J.T. Gralka
20,126 PointsWow Randy! That was a really thorough explanation.
S K,
I'm happy to see that things are moving along for you in PHP Paradise! I'm pretty sure :-) Also, according to the Treehouse roadmap page, Randy is working on a series that will premiere at the end of this month on Feb. 21.
Best,
J.T.