Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

first question on stack overflow. I have always been able to find what I am looking for without asking a question, but this has me stumped.

I am upgrading my site to hosting using Amazon cloud. As part of that upgrade, I am deploying my code into what I have labeled my E1 server (Development). I figured this would be a quick process, but am hitting what has proved to be quite a roadblock with if statements in PHP.

I use nested HTML within php if statements to show feedback to users on my site. This worked great on my previous hosting provider, PHP version 5.3.13 and the new server has PHP version 5.3.28.

Here is the code:

$positive_feedback = $_SESSION['letter_update_feedback'];

if($positive_feedback != "")
{
?>
<div class="positivefeedback">
<h3>
<?php
echo $positive_feedback;
$positive_feedback = "";
?>
</h3>
</div>
<?
}
$_SESSION['letter_update_feedback'] = $positive_feedback;

On the new server it fails with this error:

Parse error: syntax error, unexpected $end in /var/www/html/viewletter.php on line 1239

On another note, it works if I remove all native HTML from inside the if statements, and replace them with an echo.

$positive_feedback = $_SESSION['letter_update_feedback'];

if($positive_feedback != "")
{
echo '<div class="positivefeedback">
<h3>';

echo $positive_feedback;
$positive_feedback = "";

echo '</h3>
</div>';

}

$_SESSION['letter_update_feedback'] = $positive_feedback;

Thanks for all the help in advance.

share|improve this question
up vote 2 down vote accepted

When programming in PHP, webmaster can encounter an error with a message such as parse error:syntax error, unexpected $end. This error is related to a syntax error in PHP. The most probable cause of the error is a missing or a mismatched parenthesis in the PHP code. To solve the missing parenthesis error in PHP, the code has to be checked from the beginning to search for it. One way to avoid errors is to use proper indentation in the code. Once all the parentheses in the code have been set correctly, parse error: syntax error, unexpected $end will be fixed.

Parse error: syntax error, unexpected $end When you program in PHP, it is likely that you have encountered the following error:

Parse error: syntax error, unexpected $end in Command line code on line 1

What causes this error?

In fact, this error means that PHP has finished analyzing your code, but you forgot to close a symbol somewhere in your page or in those that were included.

Situations:

  1. you forgot to close a quote, so PHP is continuing to analyze your code until it finds the closing quotation mark.

  2. You forgot to close a bracket, so from the last opening, PHP considers all the code that follows as part of a block that never ends.

  3. You forgot to close a parenthesis, so from the last open parenthesis, PHP considers all the code that follows as part of a specific block (condition, arguments of functions etc) that does not end.

  4. You forgot a comma, so for PHP there is an instruction in your code that has no end.

This means that the problem may not be on the line mentioned in the error message, as the missing symbol could be anywhere after that point.

Examples of codes that cause this error.

Here are some examples of codes that are causing this error.

<?      
 if ($condition){      
    echo "true";      
?>      

Forgot to close a quote:

<?
 echo "test;
?>

Forgot to close a parenthesis:

<?      
  mysql_query("mysite", "logon", "thisisnotasqlserver.com" ;      
?>

Forget a semicolon:

<?
 if ($test){
   echo '1'
 }
?>

How to fix/avoid this error This is often due to a poorly organized presentation of your code. Especially remember to indent your code well, to visually distinguish the different blocks.

Example of a clean code

//Equivalent of array_reverse()      
function inverse_table($table)      
{      
    $ret = array();      
    if (is_array($table) ){      
        for($i=sizeof($table) - 1; $i >= 0; $i++)      
        {      
            $ret[] = $table[$i];      
        }      
    }      
    return $ret;      
}

Example of a non-indented code, harder to debug:

function inverse_table($table)      
{      
$ret = array();      
if (is_array($table) ){      
for($i=sizeof($table) - 1; $i >= 0; $i++)      
{      
$ret[] = $table[$i];      
}      
}      
return $ret;      
}

If you encounter this error, please note that it is not necessarily the end of your code, for example, omitting an accolade may be at the very beginning of your code, even if your file is 1000 lines.

share|improve this answer
    
Thank you for a detailed explanation of the error. However, this doesn't answer why it works on my previous provider and not on the new provider. It also doesn't really give me the solution, or why the page loads without error when I remove the nested HTML from the page and replace with an echo. – shannara_dl Feb 9 '14 at 5:29
    
Nevermind, it does. I was missing a php on my end tag for the html. See right where this is: "?> </h3> </div> <?" If I had properly indented my code, this would have been easier to spot. Thanks. – shannara_dl Feb 9 '14 at 5:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.