2016-03-04 50 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