php 语法错误,意外的 T_ENCAPSED_AND_WHITESPACE,需要 T_STRING 或 T_VARIABLE 或 T_NUM_STRING
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8400018/
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
syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
提问by user1083482
I have been staring at this code for hours now and I cannot figure out where my mistake is. I know this syntax error usually comes up because of some missing or out of place curly brace or some issue with single/double quotes and I'm not sure there is one anywhere in my code. I am only trying to fix my syntax right now so I can get the code to completely compile. Any help would be much appreciated. Here is my code:
我已经盯着这个代码几个小时了,我不知道我的错误在哪里。我知道这个语法错误通常是由于缺少或不合适的大括号或单/双引号的一些问题而出现的,我不确定我的代码中是否有任何地方。我现在只是想修复我的语法,这样我就可以让代码完全编译。任何帮助将非常感激。这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Add to and Read from the Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<?php
function print_form() {
echo <<<END
<form action="$_SERVER[PHP_SELF]" method="post">
<h3>Please put your comments below.</h3>
<input type="hidden" name="stage" value="process" >
<p>Name:</p>
<input type="text" size="30" name="WholeName" />
<p>Comment:</p>
<input type="text" size="200" name="Comment" />
<input type ="submit" value ="Submit" >
</form>
END;
}
function process_form() {
print "<p>adding comment...</p>";
$Name = $_POST['WholeName'];
$Comment = $_POST['Comment'];
if( preg_match("^[a-zA-Z]+$", $Name)) {
if( preg_match("^[a-zA-Z0-9]_\-\'[.?!]+$", $Comment)) {
$sql = "insert into comments1 values (
'$Name',
'$Comment')";
$result = mysql_query($sql) or die("Mysql query failed");
} else {
print "invalid name";
}
} else {
print "invalid characters";
}
}
$db = mysql_connect("", "", "");
if (!$db) {
print "Error - Could not connect to mysql";
exit;
}
$er = mysql_select_db("");
if (!$er) {
print "Error - Could not connect to comments1 database";
exit;
}
if (isset($_POST['stage']) && ('process' == $_POST['stage'])) {
process_form();
} else {
print_form();
}
?>
</body>
</html>
回答by JGurtz
I stumbled on this question as I had the same error. Mine was due to a slightly different problem and since I resolved it on my own I thought it useful to share here. Original code with issue:
我偶然发现了这个问题,因为我有同样的错误。我是由于一个稍微不同的问题,因为我自己解决了它,所以我认为在这里分享很有用。有问题的原始代码:
$comment = "$_POST['comment']";
Because of the enclosing double-quotes, the index is not dereferenced properly leading to the assignment error. In my case I chose to fix it like this:
由于包含双引号,索引未正确解除引用导致分配错误。在我的情况下,我选择这样修复它:
$comment = "$_POST[comment]";
but dropping either pair of quotes works; it's a matter of style I suppose :)
但是删除任何一对引号都有效;我想这是一个风格问题:)
回答by deceze
Your problem is that you're not closing your HEREDOC correctly. The line containing END;
must not contain any whitespace afterwards.
您的问题是您没有正确关闭 HEREDOC。包含的行END;
之后不得包含任何空格。
回答by Toto
You have extra spaces after END;
that cause the heredoc not terminated.
之后您有额外的空格END;
,导致heredoc 未终止。
回答by mat
Might be a pasting problem, but as far as I can see from your code, you're missing the single quotes around the HTML part you're echo-ing.
可能是粘贴问题,但就我从您的代码中看到的而言,您缺少回显的 HTML 部分周围的单引号。
If not, could you post the code correctly and tell us what line is causing the error?
如果没有,您能否正确发布代码并告诉我们哪一行导致错误?