I got an error:

Parse error: syntax error, unexpected end of file in the line

With this code:

<html>
    <?php
        function login()
        {
            // Login function code
        }
        if (login())
    {?>

    <h2>Welcome Administrator</h2>
    <a href=\"upload.php\">Upload Files</a>
    <br />
    <a href=\"points.php\">Edit Points Tally</a>

    <?php}
        else
        {
            echo "Incorrect login details. Please login";
        }
    ?>
    Some more HTML code
</html>

What's the problem?

share|improve this question
1  
I would imagine you have an error with some of your PHP code... which you haven't posted. – Yacoby Jul 14 '12 at 9:22
2  
It sounds like you have a missing '}', ';' or bracket but it would help if you posted you whole php code – ryanc1256 Jul 14 '12 at 9:23
    
Please post a complete example – phihag Jul 14 '12 at 9:23
1  
I have added the code – pratnala Jul 14 '12 at 9:25
7  
A year and a half hence, it is funny that this question was closed because it is too localized but it has got almost 50k views. – pratnala Nov 6 '13 at 11:19
up vote 154 down vote accepted

You should avoid this (at the end of your code):

{?>

and this:

<?php}

You shouldn't put brackets directly close to the open/close php tag, but it separate with a space:

{ ?>
<?php {

also avoid <? and use <?php

share|improve this answer
15  
In case it helps anybody: I got a similar error, but caused by <? } ?> instead of <?php } ?>. – Adam Libuša Nov 7 '13 at 13:06
41  
I had similar ptoblem but my case was that i had short_open_tag = Off in my php.ini. When i turned it to On, my code worked. – bksi Feb 12 '14 at 19:31
3  
Note while Apache accepts this Nginx does not (or rather fcgi does not) so if your stumped by why this is working on one web server and not another you know why – Sammaye Jul 18 '14 at 13:53
    
hahaha, and avoid copy pasting code which leads to errors like this – Dr Deo Sep 14 '15 at 13:20
    
The code in the OP was written completely by me. – pratnala Nov 2 '15 at 21:14

I had the same error, but I had it fixed by modifying the php.ini file.

Find your php.ini file see Dude, where's my php.ini?

then open it with your favorite editor.

Look for a short_open_tag property, and apply the following change:

; short_open_tag = Off ; previous value
short_open_tag = On ; new value
share|improve this answer
3  
This answer is especially important when dealing with legacy code. I have inherited code written by someone else, and the codebase was full of of <? ?> (note, the missing php). I am tasked to add or change features, and it typically isn't a good idea to just go around changing everything just for one or two features. Your answer really helped. – Salehen Rahman Feb 11 at 18:54
    
Good solution.... – DiniZx Sep 9 at 12:36

I had the same error, but I had it fixed by modifying the php.ini and / or editing the PHP file!

There are two different methods to get around the parse error syntax.

Method 1 (Your PHP file)

Avoid in your PHP file this:

<? } ?>

Make sure you put it like this

<?php ?>

Your code contains <? ?>

NOTE: The missing php after <?!

Method 1 (php.ini file)

There is also a simple way to solve your problem. Search for the short_open_tag property value (Use in your text editor with Ctrl + F!), and apply the following change:

; short_open_tag = Off

to

short_open_tag = On

NOTE: Reload your Server (like for example: Apache) and reload your PHP webpage in your browser.

share|improve this answer
    
This is genius. – pratnala Mar 5 at 16:43

Just go to php.ini then find short_open_tag= Off set to short_open_tag= On

share|improve this answer
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review – insertusernamehere Dec 10 '15 at 0:11

Check that you closed your class.

For example, if you have controller class with methods, and by accident you delete the final bracket, which close whole class, you will get this error.

class someControler{
private $varr;
public $varu;
..
public function method {
..
} 
..
}// if you forget to close the controller, you will get the error
share|improve this answer

Avoid this as well <? } ?> make sure you put <?php } ?>

share|improve this answer

I saw some errors, which I've fixed below.

This is what I got as being erroneous:

if (login())
{?>
<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>
<?php}
else
{
echo "Incorrect login details. Please login";
}

This is how I would have done it:

<html>
    some code
<?php
function login()
{
    if (empty ($_POST['username']))
    {
        return false;
    }
    if (empty ($_POST['password']))
    {
        return false;
    }
    $username = trim ($_POST['username']);
    $password = trim ($_POST['password']);
    $scrambled = md5 ($password . 'foo');
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
        $error = "Unable to connect to the database server";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_set_charset ($link, 'utf8'))
    {
        $error = "Unable to set database connection encoding";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_select_db ($link, 'foo'))
    {
        $error = "Unable to locate the foo database";
        include 'error.html.php';
        exit ();
    }
    $sql = "SELECT COUNT(*) FROM admin WHERE username = '$username' AND password = '$scrambled'";
    $result = mysqli_query ($link, $sql);
    if (!$result)
    {
        return false;
        exit ();
    }
    $row = mysqli_fetch_array ($result);
    if ($row[0] > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
if (login())
{
echo '<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>';
}
else
{
    echo "Incorrect login details. Please login";
}
?>
some more html code
</html>
share|improve this answer
    
Well the thing is inside if (login ()), I have lot of code to write, this is just a sample. Anyway the first answer here has solved it. I didn't know that a space has to be put. Thanks for the help! – pratnala Jul 14 '12 at 9:36
1  
yer just helping anyway yer i did get that but i just like to do it that way. And yer sure – ryanc1256 Jul 14 '12 at 9:38

protected by Community Mar 12 at 18:34

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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