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

后端 未结 16 1658
孤城傲影
孤城傲影 2020-11-22 05:27

I got an error:

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

With this code:


    

        
相关标签:
16条回答
  • 2020-11-22 05:38

    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 讨论(0)
  • 2020-11-22 05:44

    Also, watch out for heredoc closing identifiers.

    Invalid Example:

    function findAll() {
        $query=<<<SQL
            SELECT * FROM `table_1`;
        SQL;
        // ... omitted
    }
    

    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
        // ... omitted
    }
    
    0 讨论(0)
  • 2020-11-22 05:45

    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 讨论(0)
  • 2020-11-22 05:46

    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.

    0 讨论(0)
  • 2020-11-22 05:47

    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>
    
    0 讨论(0)
  • 2020-11-22 05:48

    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 () {
    
    };
    
    0 讨论(0)
提交回复
热议问题