2017-04-19 228 views
-1

我在爲一個項目編寫一個簡單的PHP驗證代碼,我正在驗證它的名字,姓氏和電子郵件。我得到這個錯誤,並且我查看了它至少45分鐘,並且無法弄清楚它是什麼。解析錯誤:語法錯誤,第44行出現意外的'{'

<?php 
$fnameErr = $lnameErr = $emailErr = ""; 
$firstname = $lastname = $email = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
if (empty($_POST["firstname"])) { 
$fnameErr = "First name is required"; 
} 

這是線38-46

+0

檢查代碼仔細,在這個錯誤說你的代碼有一個不需要的{符號。 –

回答

1

看起來像括號中的一個還沒有被關閉。

<?php 
    $fnameErr = $lnameErr = $emailErr = ""; 
    $firstname = $lastname = $email = ""; 

    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     if (empty($_POST["firstname"])) { 
      $fnameErr = "First name is required"; 
     } 
    } // Add an ending brace 
0

使用IDE來幫助您解決語法錯誤。我認爲你是在學習階段,所以我建議你使用一個IDE(Eclipse或Netbeans的)

缺少括號:

$fnameErr = $lnameErr = $emailErr = ""; 
$firstname = $lastname = $email = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
if (empty($_POST["firstname"])) { 
$fnameErr = "First name is required"; 
} 
}//Here 
1

你已經錯過了結束第二個,如果條件。代碼應該是這樣的。

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["firstname"])) { 
     $fnameErr = "First name is required"; 
    } 
}//this } symbol you have missed. 
1

你錯過了你的第二個條件支架,你可以更新您的代碼將更具可讀性

<?php 
// this is error variable 
$fnameErr = ""; 
$lnameErr = ""; 
$emailErr = ""; 

// this is post initial variable define 
$firstname = ""; 
$lastname = ""; 
$email = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["firstname"])) { 
    $fnameErr = "First name is required"; 
    } 
} 
?> 

瞭解更多信息

PHP Parse/Syntax Errors; and How to solve them?

相關問題