解析错误:语法错误,我的 PHP 代码中出现意外的文件结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11482527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse error: Syntax error, unexpected end of file in my PHP code
提问by pratnala
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?
有什么问题?
回答by j0k
You should avoid this (at the end of your code):
您应该避免这种情况(在代码末尾):
{?>
and this:
和这个:
<?php}
You shouldn't put brackets directly close to the open/close phptag, but separate it with a space:
你不应该把括号直接靠近打开/关闭php标签,而是用空格分隔它:
{ ?>
<?php {
also avoid <?and use <?php
也避免<?和使用<?php
回答by Archana Balachandran
I had the same error, but I had it fixed by modifying the php.inifile.
我有同样的错误,但我通过修改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_tagproperty, and apply the following change:
查找short_open_tag属性,并应用以下更改:
; short_open_tag = Off ; previous value
short_open_tag = On ; new value
回答by Suriyaa
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)
方法一(你的PHP文件)
Avoid in your PHP file this:
避免在您的 PHP 文件中:
<? } ?>
Make sure you put it like this
确保你把它像这样
<?php ?>
Your code contains
<? ?>NOTE:The missing
phpafter<?!
您的代码包含
<? ?>注意:后遗失!
php<?
Method 2 (php.ini file)
方法二(php.ini文件)
There is also a simple way to solve your problem.
Search for the short_open_tagproperty 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_tagallows you to use the short open tag (<?) although this might cause issues when used with xml (<?xmlwill 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 网页。
回答by Hamzah Akram
Just go to php.ini then find short_open_tag= Offset to short_open_tag= On
只需转到 php.ini 然后找到short_open_tag= Off设置为short_open_tag= On
回答by Imtiaz
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
希望它可以帮助某人
回答by olga
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
回答by Steve Wasiura
also, look for a comment // that breaks the closing curly brace
另外,寻找一个注释 // 打破右花括号
if (1==1) { //echo "it is true"; }
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 也不能正确处理剩余的代码。
回答by Gpak
Avoid this as well <? } ?>make sure you put <?php } ?>
避免这种情况以及<? } ?>确保你把<?php } ?>
回答by ryanc1256
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>
回答by J. Wrong
In my case the culprit was the lone opening <?phptag in the last line of the file. Apparently it works on some configurations with no problems but causes problems on others.
在我的情况下,罪魁祸首是<?php文件最后一行中唯一的开始标签。显然它适用于某些配置没有问题,但会导致其他配置出现问题。

