简体   繁体   English

解析错误:语法错误,我的 PHP 代码中的文件意外结束

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

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?有什么问题?

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标签,而是用空格分隔:

{ ?>
<?php {

also avoid <?也避免<? and use <?php并使用<?php

I had the same error, but I had it fixed by modifying the php.ini file.我有同样的错误,但我通过修改php.ini文件修复了它。

Find your php.ini file see Dude, where's my php.ini?找到你的 php.ini 文件,看看老兄,我的 php.ini 呢?

then open it with your favorite editor.然后用你最喜欢的编辑器打开它。

Look for a short_open_tag property, and apply the following change:查找short_open_tag属性,并应用以下更改:

; short_open_tag = Off ; previous value
short_open_tag = On ; new value

I had the same error, but I had it fixed by modifying the php.ini and / or editing the PHP file!我有同样的错误,但我通过修改 php.ini 和/或编辑 PHP 文件来修复它!

There are two different methods to get around the parse error syntax.有两种不同的方法可以绕过解析错误语法。

Method 1 (Your PHP file)方法 1(您的 PHP 文件)

Avoid in your PHP file this:避免在您的 PHP 文件中:

<? } ?>

Make sure you put it like this确保你把它像这样

<?php ?>

Your code contains <? ?>您的代码包含<? ?> <? ?>

NOTE: The missing php after <?注意: php <? !

Method 2 (php.ini file)方法二(php.ini 文件)

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属性值(在您的文本编辑器中使用Ctrl + F !) ,并应用以下更改:

; 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)!根据核心 php.ini 指令的描述short_open_tag允许您使用短打开标记( <? ),尽管这可能会在与 xml 一起使用时引起问题(启用此功能时<?xml将不起作用)!

NOTE: Reload your Server (like for example: Apache) and reload your PHP webpage in your browser.注意:重新加载您的服务器(例如:Apache)并在您的浏览器中重新加载您的 PHP 网页。

只需转到 php.ini 然后找到short_open_tag= Off设置为short_open_tag= On

Also, watch out for heredoc closing identifiers.此外,请注意heredoc 关闭标识符。

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.其中数字 5 可能是文件的最后一行。

According to php manual :根据php手册

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.这是在 UNIX 系统上的 \\n,包括 macOS。 The closing delimiter must also be followed by a newline.结束分隔符后还必须跟一个换行符。

TLDR : Closing identifiers should NOT be indented. TLDR :结束标识符不应缩进。

Valid Example:有效示例:

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

    // ...
}

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: 标记未关闭时,我遇到了这个麻烦。

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

Closing it using the following solved the syntax error: unexpected end of file使用以下方法关闭它解决了语法错误: unexpected end of file

<?php endforeach; ?>

Hope it helps someone希望它可以帮助某人

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

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.右花括号不会正确关闭条件部分,php 也不会正确处理其余代码。

Avoid this as well <? } ?>也避免这种情况<? } ?> <? } ?> make sure you put <?php } ?> <? } ?>确保你把<?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>

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.上面的文件返回Parse error: syntax error, unexpected end of file in而下面没有。

<?php
function () {

};

In my case the culprit was the lone opening <?php tag in the last line of the file.在我的情况下,罪魁祸首是文件最后一行中唯一的打开<?php标记。 Apparently it works on some configurations with no problems but causes problems on others.显然,它适用于某些配置而没有问题,但会导致其他配置出现问题。

For me, the most frequent cause is an omitted } character, so that a function or if statement block is not terminated.对我来说,最常见的原因是省略了 } 字符,因此函数或 if 语句块没有终止。 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.如果这对任何人有帮助,请投票给答案。

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.为了补充其他答案,如果您使用像 FileZilla 这样的 ftp 客户端,也可能是由于您的 php 脚本的自动缩小。 Ensure that the transfer type is set to Binary and not ASCII or auto.确保传输类型设置为二进制而不是 ASCII 或自动。 The ASCII or auto transfer type can minify your php code leading to this error. ASCII 或自动传输类型可以缩小导致此错误的 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'在我的情况下,我有一个错误“解析错误:语法错误,第 665 行我的 PHP 代码中的文件意外结束”在我的情况下,问题在于关闭“html”

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

In version 7.2 closing tag should be WITHOUT spaces before在 7.2 版本中,结束标记之前应该没有空格

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

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我希望有人受益

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如果您是 linux 用户并在apache2服务器上运行您的旧 php 网站,则找到此文件/etc/php/<php version>/apache2/php.in ,以防您使用php cli执行 php 脚本,例如php example.php然后/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.serviceshort_open_tag = Off设置为short_open_tag = On并在 apache2 的情况下重新启动您的服务sudo systemctl restart apache2.service

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.如果您使用 parse_ini_file($file) 或例程正在处理 .ini 文件,请检查您的数据是否在 ini 文件中被引用。 Unquoted data will cause this error.未引用的数据将导致此错误。 Ex;前任; data1=test will cause the error, data1="test" will not. data1=test 会导致错误,data1="test" 不会。

I developed a plugin and installed it on a Wordpress site running on Nginx and it was fine.我开发了一个插件并将它安装在一个运行在 Nginx 上的 Wordpress 站点上,它很好。 I only had this error when I switched to Apache, turned out the web server was not accepting the <?当我切换到 Apache 时,我只有这个错误,结果 Web 服务器不接受<? , so I just replaced the <? ,所以我只是替换了<? tags to <?php then it worked.标签到<?php然后它工作。

You can't divide IF/ELSE instructions into two separate blocks.您不能将 IF/ELSE 指令分成两个单独的块。 If you need HTML code to be printed, use echo .如果您需要打印 HTML 代码,请使用echo

<html>
    <?php
        function login()
        {
            // Login function code
        }
        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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM