如果 PHP 中的语句尝试冒号和大括号仍然需要帮助


if statement in php tried colons and curly brackets still need assistance

我有一个页面,我正在尝试让我的搜索框执行操作。当有搜索时,我有3个网站:Google,wiki和duckduckgo。当我将页面上传到服务器时,我收到语法错误。我以前从未在 PHP 中做过 if 语句。我看了w3schools的教程,我仍然感到困惑。我尝试过冒号和大括号,但我仍然感到困惑。

有人可以给我正确的方向提示吗?

<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;
    }
}
?>

你用了 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
}

尝试这样做。 如果你在 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