PHP:语法错误,意外的 T_STRING
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10783733/
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
PHP: syntax error, unexpected T_STRING
提问by cassmoney
I am getting a syntax error for this PHP code
我收到此 PHP 代码的语法错误
syntax error, unexpected T_STRING
It says line 2, which is the include line:
它说第 2 行,即包含行:
<?php ?
?include 'config.inc.php'; ?
?
?// initialization ?
?$photo_upload_fields = ''; ?
?$counter = 1; ?
?
?// If we want more fields, then use, preupload.php?number_of_fields=20 ?
?$number_of_fields = (isset($_GET['number_of_fields'])) ? ?
? ?(int)($_GET['number_of_fields']) : 5; ?
?
?// Firstly Lets build the Category List ?
?$result = mysql_query('SELECT category_id,category_name FROM gallery_category'); ?
?while($row = mysql_fetch_array($result)) { ?
? ?$photo_category_list .= <<<__HTML_END
<option value="$row[0]">$row[1]</option>\n ?
__HTML_END; ?
?} ?
?mysql_free_result( $result ); ? ?
?
?// Lets build the Image Uploading fields ?
?while($counter <= $number_of_fields) { ?
? ?$photo_upload_fields .= <<<__HTML_END ?
<tr><td> ?
?Photo {$counter}: ?
?<input name="photo_filename[]" ?
type="file" /> ?
</td></tr> ?
<tr><td> ?
?Caption: ?
?<textarea name="photo_caption[]" cols="30" ?
? ?rows="1"></textarea> ?
</td></tr> ?
__HTML_END; ?
? ?$counter++; ?
?} ?
?
?// Final Output ?
?echo <<<__HTML_END ?
<html> ?
<head> ?
<title>Lets upload Photos</title> ?
</head> ?
<body> ?
<form enctype="multipart/form-data" ?
?action="upload.php" method="post" ?
?name="upload_form"> ?
?<table width="90%" border="0" ?
? ?align="center" style="width: 90%;"> ?
? ?<tr><td> ?
? ? ?Select Category ?
? ? ?<select name="category"> ?
? ? ?$photo_category_list ?
? ? ?</select> ?
? ?</td></tr> ?
? ?<! - Insert the image fields here --> ?
? ?$photo_upload_fields ?
? ?<tr><td> ?
? ? ?<input type="submit" name="submit" ?
? ? ? ?value="Add Photos" /> ?
? ?</td></tr> ?
?</table> ?
</form> ?
</body> ?
</html> ?
__HTML_END; ?
?>
My include file looks like this:
我的包含文件如下所示:
<?php
$mysql_link = mysql_connect("localhost","user","password");
mysql_select_db("db_name") or die("Could not select database");
$images_dir = "photos";
?>
Solution:
解决方案:
I had invisible unicode characters hidden in the code which caused the PHP parser to complain of the above syntax error.
我在代码中隐藏了不可见的 unicode 字符,这导致 PHP 解析器抱怨上述语法错误。
回答by user2428118
You have multiple spaces at the start and the end of your HEREDOC syntax variables; this leads to problems. Remove the spaces after
在 HEREDOC 语法变量的开头和结尾处有多个空格;这会导致问题。去掉后面的空格
<<<__HTML_END
and after
之后
__HTML_END;
and your code should be working.
并且您的代码应该可以正常工作。
See also this thread where I found the answer: http://board.phpbuilder.com/board/showthread.php?t=10333424
另请参阅我找到答案的线程:http: //board.phpbuilder.com/board/showthread.php?t=10333424
回答by Darren Craig
Delete the first few lines and rewrite them. Sometimes if you copy and paste code, it can insert unicode invisible characters, which aren't parsed correctly by PHP. So it throws this error. It's happened to me several times.
删除前几行并重写它们。有时,如果您复制和粘贴代码,它会插入 unicode 不可见字符,PHP 无法正确解析这些字符。所以它抛出这个错误。它发生在我身上好几次了。
回答by WebsiteDesignAndSupport
just ensure you have closed all you " and ' in the file you are including. usually this error is pointed to the first line in the file.
只需确保您已关闭包含的文件中的所有 " 和 '。通常此错误指向文件中的第一行。
回答by Harshal
Use the inclue as: include("config.inc.php");for right syntax.
使用 inclue as:include("config.inc.php");以获得正确的语法。

