本文介绍了语法错误,意外的T_ENCAPSED_AND_WHITESPACE,预期为T_STRING或T_VARIABLE或T_NUM_STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经盯着这个代码好几个小时了,但我不知道我的错误在哪里.我知道这种语法错误通常是由于缺少大括号或错位或单/双引号引起的,因此我不确定代码中是否有任何错误.我现在仅尝试修复语法,以便使代码完全编译.任何帮助将非常感激.这是我的代码:

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>

推荐答案

您的问题是您没有正确关闭HEREDOC.包含END; 的行中不得包含任何空格之后 .

Your problem is that you're not closing your HEREDOC correctly. The line containing END; must not contain any whitespace afterwards.

这篇关于语法错误,意外的T_ENCAPSED_AND_WHITESPACE,预期为T_STRING或T_VARIABLE或T_NUM_STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:50