PHP 解析错误:语法错误,文件意外结束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19745322/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:57:20  来源:igfitidea点击:

PHP Parse error: syntax error, unexpected end of file

php

提问by user2948326

I am very new to PHP and somehow got thrown into an advanced level course. My instructor will not offer any help since it is an advanced course. I will post my code below in hopes that someone can at least point me in the right direction for a fix. This is only the first part of the assignment, but not worth going any further if I cannot get this part to work. I believe I have check all the braces and quotes, but I sure could have missed something or ?????.

我对 PHP 很陌生,不知何故被扔进了高级课程。我的导师不会提供任何帮助,因为这是一门高级课程。我将在下面发布我的代码,希望有人至少能指出我正确的方向进行修复。这只是作业的第一部分,但如果我不能让这部分工作,就不值得再继续了。我相信我已经检查了所有的大括号和引号,但我肯定可能遗漏了一些东西或 ?????。

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// define variables and set to empty values
$name1Err = $name2Err = $dobErr = $stateErr = $zipErr = "";
$name1 = $name2 = $dob = $state = $zip = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   if (empty($_POST["name1"]))
     {$name1Err = "First Name is required";}
   else
     {
     $name1 = test_input($_POST["name1"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name1))
       {
       $name1Err = "Only letters and white space allowed";
       }
     }

   if (empty($_POST["name2"]))
     {$name2Err = "Name is required";}
   else
     {
     $name2 = test_input($_POST["name2"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name2))
       {
       $name2Err = "Only letters and white space allowed";
       }
     }

   if (empty($_POST["dob"]))
     {$dobErr = "Date Of Birth is required";}
   else
     {
     $dob = test_input($_POST["dob"]);
     // check if date of birth format is valid
     if (!preg_match("/^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}$/",$dob))
       {
       $dobErr = "Invalid date format";
       }
     }

   if (empty($_POST["state"]))
     {$stateErr = "2 Digit State Abbreviation is required";}
   else
     {
     $state = test_input($_POST["state"]);
     // match two uppercase letters with definite (one or more) whitespace on either side)
     if (!preg_match("/\s+[A-Z]{2}\s+/ ",$state))
       {
       $stateErr = "Invalid State Abbreviation";
   }
     }

   if (empty($_POST["zip"]))
     {$zipErr = "Zip Code is required";}
   else
     {
     $zip = test_input($_POST["zip"]);
     // Validates Zip Code (5 digits plus optional -4)
     if (!preg_match("^\d{5}(-\d{4})?$",$zip))
       {
       $zipErr = "Only letters and white space allowed";
       }
     }


function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>

<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   First Name: 
     <input type="text" name="name1">
   <span class="error">* <?php echo $name1Err;?></span>
   <br><br>
   Last Name: 
   <input type="text" name="name2">
   <span class="error">* <?php echo $name2Err;?></span>
   <br><br>
   Date Of Birth: 
   <input type="text" name="dob">
   <span class="error"><?php echo $dobErr;?></span>
   <br><br>
   State: 
     <input type="text" name="state">
   <span class="error">* <?php echo $stateErr;?></span>
   <br><br>
   Zip Code: 
   <input type="text" name="zip">
   <span class="error">* <?php echo $zipErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit">
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name1;
echo "<br>";
echo $name2;
echo "<br>";
echo $dob;
echo "<br>";
echo $state;
echo "<br>";
echo $zip;
?>
</body>

</html>

回答by Sumit Bijvani

You are missing to close you if..statement.

你错过了关闭你的if..声明。

 if ($_SERVER["REQUEST_METHOD"] == "POST")
 {  
    // your code
 }
^^^

Use Better Text Editor to get rid from this type of SYNTAX errors

使用 Better Text Editor 摆脱这种类型的 SYNTAX 错误