Parse error: Syntax error, unexpected end of file

2018-12-31 09:00发布

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?

11条回答
路过你的时光
2楼-- · 2018-12-31 09:16

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

查看更多
梦寄多情
3楼-- · 2018-12-31 09:16

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
查看更多
低头抚发
4楼-- · 2018-12-31 09:22

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

查看更多
伤终究还是伤i
5楼-- · 2018-12-31 09:24

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

查看更多
初与友歌
6楼-- · 2018-12-31 09:26

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.

查看更多
只若初见
7楼-- · 2018-12-31 09:27

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

查看更多
登录 后发表回答