2016-03-04 39 views
0

我有一個頁面,我試圖讓我的搜索框執行操作。當有搜索時,我有3個網站:Google,wiki和duckduckgo。當我將頁面上傳到服務器時,我收到語法錯誤。我從來沒有在PHP中做過if語句。我看着w3schools中的教程,我仍然感到困惑。我試過冒號和大括號,我仍然感到困惑。如果語句在PHP試圖冒號和花括號仍然需要幫助

有人能給我一個正確的方向?

<form action="search.php" method="get"> 

<?php 
$site = substr(filter_input(INPUT_GET, 'site', FILTER_SANITIZE_STRING), 0, 8); 
$terms = substr(filter_input(INPUT_GET, 'terms', FILTER_SANITIZE_STRING), 0, 25); 

if ($site=="google") header('Location:https://www.google.com/#q=' . $terms); { 
}else{ 
    if ($site=="Wikipedia")header ('Location:https://www.wikipedia.org/#q=' . $terms); { 

    } else ($site=="DuckDuckGo")header ('Location:https://duckduckgo.com/#q=' .$terms); 
     endif; 
    } 
} 
?> 
+0

參見手冊關於if語句:http://php.net/manual/en/control-structures.if.php – Rizier123

+1

@tawanna:你應該檢查,如果在進入這裏之前,還有其他PHP語法。 –

回答

1

您已經使用了if else語法,這是非常錯誤的。如果一個陳述是正確的,總是要完成的任務應該放在括號內。

替代

if ($site=="google") header('Location:https://www.google.com/#q=' . $terms);{ 
}else{ 
if ($site=="Wikipedia")header  ('Location:https://www.wikipedia.org/#q=' . $terms);{ 
} else ($site=="DuckDuckGo")header ('Location:https://duckduckgo.com/#q=' .$terms); 
endif; 
} 

與以下。這是正確的語法

if ($site=="google"){ 
    header('Location:https://www.google.com/#q=' .  $terms); 
}elseif ($site=="Wikipedia"){ 
    header ('Location:https://www.wikipedia.org/#q=' . $terms); 
}elseif ($site=="DuckDuckGo"){ 
    header ('Location:https://duckduckgo.com/#q=' .$terms); 
}else{ 
    //if $site doesn't match any of the above 
} 
0

嘗試這樣。如果你在if下面有一行。則忽略該項花{}括號

 if ($site=="google") 
      header('Location:https://www.google.com/#q=' . $terms); 
     else if ($site=="Wikipedia") 
      header ('Location:https://www.wikipedia.org/#q=' . $terms); 
     else if($site=="DuckDuckGo") 
      header ('Location:https://duckduckgo.com/#q=' .$terms); 
     else 
      //if your site == google not work