210

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?

10
  • 3
    I would imagine you have an error with some of your PHP code... which you haven't posted.
    – Yacoby
    Commented Jul 14, 2012 at 9:22
  • 8
    It sounds like you have a missing '}', ';' or bracket but it would help if you posted you whole php code
    – ryanc1256
    Commented Jul 14, 2012 at 9:23
  • 25
    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
    Commented Nov 6, 2013 at 11:19
  • 4
    I agree...this totally fixed my problem. Not localized at all in regards to the question and the solution. Commented Sep 2, 2016 at 9:35
  • 7
    Also check for short_tags in your php.ini under BOTH apache and cli configs
    – AO_
    Commented Nov 1, 2016 at 14:40

20 Answers 20

369

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 separate it with a space:

{ ?>
<?php {

also avoid <? and use <?php

7
  • 58
    In case it helps anybody: I got a similar error, but caused by <? } ?> instead of <?php } ?>. Commented Nov 7, 2013 at 13:06
  • 76
    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
    Commented Feb 12, 2014 at 19:31
  • 5
    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
    Commented Jul 18, 2014 at 13:53
  • 1
    Can you add to avoid also this <?php //echo $pagetitle; ?> the comment slashes are killing the closing ?> Commented Nov 27, 2015 at 17:37
  • 1
    Is there a reason <? and <?= should be avoided? I was taught them as useful shorthands. Commented Jan 20, 2021 at 0:26
117

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
2
  • 11
    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.
    – Sal Rahman
    Commented Feb 11, 2016 at 18:54
  • 1
    Spot on, this is especially noteworthy if you have upgraded to the newer Plesk hosting software for instance or upgraded your PHP version. We found the php.ini settings got trashed and we had to go through set them again, this particular one was required to avoid the unexpected end of file error
    – Trevor
    Commented Oct 17, 2017 at 20:33
40

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 2 (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

According to the description of core php.ini directives, short_open_tag allows you to use the short open tag (<?) although this might cause issues when used with xml (<?xml will not work when this is enabled)!

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

0
15

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

0
14

Also, watch out for heredoc closing identifiers.

Invalid Example:

// it's not working!!!

function findAll() {
    $query=<<<SQL
        SELECT * FROM `table_1`;
    SQL; // <-------- THIS IS BAD

    // ...
}

This will throw an exception that resembles the following:

<br />
<b>Parse error</b>:  syntax error, unexpected end of file in <b>[...][...]</b> on line <b>5</b><br />

where number 5 might be the last line number of your file.

According to php manual:

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.

TLDR: Closing identifiers should NOT be indented.

Valid Example:

function findAll() {
    $query=<<<SQL
        SELECT * FROM `table_1`;
SQL;
    // closing identifier should not be indented, although it might look ugly

    // ...
}
2
  • 3
    Thank you so much! This was it for me after seemingly endless matching of opening and closing tags. Commented Sep 24, 2020 at 18:20
  • I had a tab character on the same line as the close of my heredoc. It was really hard to see!
    – George
    Commented Nov 9, 2022 at 17:19
8

Look for any loops or statements are left unclosed.

I had ran into this trouble when I left a php foreach: tag unclosed.

<?php foreach($many as $one): ?>

Closing it using the following solved the syntax error: unexpected end of file

<?php endforeach; ?>

Hope it helps someone

0
6

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
0
5

also, look for a comment // that breaks the closing curly brace

if (1==1) { //echo "it is true"; }

the closing curly brace will not properly close the conditional section and php won't properly process the remainder of code.

0
4

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

3

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>
2
  • 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
    Commented Jul 14, 2012 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
    Commented Jul 14, 2012 at 9:38
3

In my case the culprit was the lone opening <?php tag in the last line of the file. Apparently it works on some configurations with no problems but causes problems on others.

3

if you are linux user and running your legacy php website on apache2 server , then locate this file /etc/php/<php version>/apache2/php.in and in case you are executing php script using php cli like php example.php then /etc/php/<php version>/cli/php.ini

set short_open_tag = Off to short_open_tag = On and restart your service in case of apache2 sudo systemctl restart apache2.service

2

Also, another case where it is hard to spot is when you have a file with just a function, I know it is not a common use case but it is annoying and had to spot the error.

<?php
function () {

}

The file above returns the erro Parse error: syntax error, unexpected end of file in while the below does not.

<?php
function () {

};
2

This is not an answer to your code, but an answer to the same error message. It may be helpful for someone. In my case I had an error 'Parse error: Syntax error, unexpected end of file in my PHP code on line 665' The problem in my case was on the line with closing 'html'

    $html = <<<html
....
            html

In version 7.2 closing tag should be WITHOUT spaces before

    $html = <<<html
....
html
1

To supplement other answers, it could also be due to the auto-minification of your php script if you are using an ftp client like FileZilla. Ensure that the transfer type is set to Binary and not ASCII or auto. The ASCII or auto transfer type can minify your php code leading to this error.

1

In my case, it was an unclosed function. I had to remove and restore functions one by one to check which one is affecting the code.

I hope someone benefits

1

For me, the most frequent cause is an omitted '}' character, so that a function or control statement block is not terminated.

Here are is a way to find such a problem:

Step 1:

Create a backup of your source file.

Step 2:

Starting from the front of the file, moving forward (or from the back of the file moving backward), remove an arbitrary section of PHP code that starts with a function or control statement block.

Step 3::

Test your code in your usual way or by using an online PHP code checker. If the error message disappears, then the last removed section of PHP code contains a function or control statement block that was unterminated. Add the "}" termination. If the error message remains, go to Step 2 and continue in the same direction.

0

If your using parse_ini_file($file) or a routine is rading an .ini file, check if you data is quoted in the ini file. Unquoted data will cause this error. Ex; data1=test will cause the error, data1="test" will not.

0

I developed a plugin and installed it on a Wordpress site running on Nginx and it was fine. I only had this error when I switched to Apache, turned out the web server was not accepting the <?, so I just replaced the <? tags to <?php then it worked.

0

I didn't see it mentioned, but if you get this after creating a new file it may be an issue with the File Encoding. Make sure it's set to UTF-8 or whatever your system expects.

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