2013-04-10 113 views
0

我是PHP新手,我面臨語法錯誤。PHP解析語法錯誤

代碼:

<?php 

    // configuration 
    require("../includes/config.php"); 

    // if form was submitted 
    if ($_SERVER["REQUEST_METHOD"] == "POST") 
    { 
     if(empty($_POST['username'])) || empty($_POST['password'] || $_POST['password'] != $_POST['confirmation']){ 
      apologize('You did something wrong!'); 
     } 
    } 
    else 
    { 
     // else render form 
     render("register_form.php", ["title" => "Register"]); 
    } 

?> 

錯誤:

Parse error: syntax error, unexpected '||' (T_BOOLEAN_OR), expecting ')' in /home/jharvard/vhosts/localhost/html/register.php on line 9

我很新的PHP,這是可能的這段代碼有多個錯誤。 作爲參考,道歉只是簡單地渲染一個抱歉的消息以及額外的輸入,渲染只是一個簡化渲染過程的模板。

+2

'如果(空($ _ POST [ '名']))'你有一個額外的括號封閉的if語句 – Pankrates 2013-04-10 14:47:53

+0

感謝,定了! – 2013-04-10 14:49:45

+0

PHP錯誤幾乎總會給你足夠的信息來發現問題。如果你學會閱讀錯誤,你永遠不會有語法問題。 – Narek 2013-04-10 14:50:01

回答

3

括號太多。而不是

if(empty($_POST['username'])) || empty($_POST['password'] || $_POST['password'] != $_POST['confirmation']){ 
          ^      ^
          too much here    too less here 

你應該有

if(empty($_POST['username']) || empty($_POST['password']) || $_POST['password'] != $_POST['confirmation']) { 
+0

你缺少一個括號 – scones 2013-04-10 14:49:18

+0

謝謝,你和惡作劇指出了這兩個錯誤:D – 2013-04-10 14:50:19

5

變化:

if(empty($_POST['username'])) || empty($_POST['password'] || $_POST['password'] != $_POST['confirmation']){ 

到:

if(empty($_POST['username']) || empty($_POST['password']) || $_POST['password'] != $_POST['confirmation']){ 
         ^      ^
+0

不只是發佈兩個不同的單行字代碼。使用第二行加上'^^^'來表示你在哪裏做了改變。 – 2013-04-10 14:49:36

0

這是因爲太多的paranethesis的。試試這個,

if(empty($_POST['username']) || empty($_POST['password']) || ($_POST['password'] != $_POST['confirmation'])){ 
     apologize('You did something wrong!'); 
    }