Parse error: Syntax error, unexpected end of file in my PHP code

PhpHtmlParse Error

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Find your php.ini file see https://stackoverflow.com/questions/8684609/dude-wheres-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

Solution 3 - Php

> 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.

Solution 4 - Php

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

Solution 5 - Php

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

    // ...
}

Solution 6 - Php

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

Solution 7 - Php

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

Solution 8 - Php

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.

Solution 9 - Php

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

Solution 10 - Php

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>

Solution 11 - Php

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 () {
    
};

Solution 12 - Php

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.

Solution 13 - Php

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

Solution 14 - Php

For me, the most frequent cause is an omitted } character, so that a function or if statement block is not terminated. I fix this by inserting a } character after my recent edits and moving it around from there. I use an editor that can locate opening brackets corresponding to a closing bracket, so that feature helps, too (it locates the function that was not terminated correctly).

We can hope that someday language interpreters and compilers will do some work to generate better error messages, since it is so easy to omit a closing bracket.

If this helps anyone, please vote the answer up.

Solution 15 - Php

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.

Solution 16 - Php

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

Solution 17 - Php

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

Solution 18 - Php

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.

Solution 19 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpratnalaView Question on Stackoverflow
Solution 1 - Phpj0kView Answer on Stackoverflow
Solution 2 - PhpArchana BalachandranView Answer on Stackoverflow
Solution 3 - PhpSuriyaaView Answer on Stackoverflow
Solution 4 - PhpHamzah AkramView Answer on Stackoverflow
Solution 5 - Phpglinda93View Answer on Stackoverflow
Solution 6 - PhpImtiazView Answer on Stackoverflow
Solution 7 - PhpolgaView Answer on Stackoverflow
Solution 8 - PhpSteve WasiuraView Answer on Stackoverflow
Solution 9 - PhpGpakView Answer on Stackoverflow
Solution 10 - Phpryanc1256View Answer on Stackoverflow
Solution 11 - PhpneisantosView Answer on Stackoverflow
Solution 12 - PhpJ. WrongView Answer on Stackoverflow
Solution 13 - Phpvivek pawarView Answer on Stackoverflow
Solution 14 - PhpDavid SpectorView Answer on Stackoverflow
Solution 15 - Phpuser3585550View Answer on Stackoverflow
Solution 16 - PhpgiacoderView Answer on Stackoverflow
Solution 17 - PhpThabo ThelediView Answer on Stackoverflow
Solution 18 - PhpWynnView Answer on Stackoverflow
Solution 19 - PhpNuhaView Answer on Stackoverflow