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
Leon Kenedy
Courses Plus Student 9,149 PointsPHP Parse Error
I keep receiving a Parse error in my PHP code in which the resulting error is: unexpected end of file php
Here is the code.
<?php
if ($_SERVER ["REQUES_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";
$email_body = $email_body . "Message: " . $message;
$pageTitle = "Contact Mike";
$section = "contact";
// Send E-Mail
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 E-Mail! I’ll be in touch shortly.
</p>
<?php
} else {
?>
<p>
I’d to hear from you! Complete the form to send me an E-Mail!
</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"> E-Mail </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="Submit">
</form>
</div>
</div>
<?php>
};
?>
<?php
include ("INC/Footer.php");
?>
2 Answers
Chris McKnight
Courses Plus Student 11,045 PointsThe ultimate problem here is that a curly brace is missing. The last curly brace in your program (which also has a semicolon after it) isn't actually run through PHP. It would be output the page but the parse error happens since a curly brace is missing for the else block. You need to add a PHP opening tag before this curly brace. Also, I suggest removing the semicolon since it's optional in the case of code blocks, such as, switch, for, if statements, etc.
You also have some logic issues. It should be comparing $_SERVER["REQUEST_METHOD"] in your first if statement. You need to change AND to && in your conditional. AND means "do the first thing and do the second thing". You actually want both things to return true to run the code inside of the if block.
if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
Leon Kenedy
Courses Plus Student 9,149 PointsOnce you sumbit the form it acts as it is redirecting then it spits out the error page.
Leon Kenedy
Courses Plus Student 9,149 PointsLeon Kenedy
Courses Plus Student 9,149 PointsI am getting Error Page 404 not found.
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsThis is copied and pasted from the first anonymous comment in the php docs for operators:
I'm not sure replacing AND with && should make a difference.. But I may be wrong!
It's very difficult to debug your code without a little more feedback. I would take a minute to break at certain points to find out how far the code is getting before it breaks. A simple:
will do the trick. Try placing it inside the if / else blocks - where is the code going - is your redirect working etc..
Chris McKnight
Courses Plus Student 11,045 PointsChris McKnight
Courses Plus Student 11,045 PointsI would need some more feedback on why it's a 404. This can happen for a number of reasons ranging from incorrect server configuration to the file/page is missing.
The updated code sample now has a stray
>. This will also generate a parser error.I pointed out
ANDto save some possible frustrations in the future. The operatorsANDandORare meant for control flow and not boolean operators. TheANDcontrol flow operates by checking left hand expression for a truthy value. If the left hand expression contains a truthy value the right hand expression will run. Notice this is different from&&because&&will evaluate the left hand and right hand expressions. Similarly, theORcontrol flow operator evaluates the left hand expression. If the left hand expression is a falsy value, the right hand expression is run. The boolean operator||actually works differently in different languages. In languages that support short circuiting, the expressions are evaluated from left to right until a truthy value is returned. If no truthy value is returned, the expression is false. Languages without short circuiting evaluation, evaluate all expressions and determine if one of them is a truthy value.For reference of operator precedence, see http://php.net/manual/en/language.operators.precedence.php. Avdi Grimm also has a nice article about these operators in ruby that is still relevant to PHP. See http://php.net/manual/en/language.operators.precedence.php.
Leon Kenedy
Courses Plus Student 9,149 PointsLeon Kenedy
Courses Plus Student 9,149 PointsApache is running correctly, although, once I submit the form I will get the Error 404 Page. I have no clue why this would be.
Chris McKnight
Courses Plus Student 11,045 PointsChris McKnight
Courses Plus Student 11,045 PointsDoes the page redirect or does it not process it at all?
Leon Kenedy
Courses Plus Student 9,149 PointsLeon Kenedy
Courses Plus Student 9,149 PointsIt redirects then fails.