比较PHP中的URL字符串

比较PHP中的URL字符串,php,string,comparison,Php,String,Comparison,我试图使用GET url中的查询来确定页面的内容。 这就是我所拥有的(为了清晰起见,对句子进行了编辑): 有比$SERVER['query\u string']更好的解析查询字符串的方法,具体来说,您可以使用$\u GET访问特定参数。示例:www.Example.com?name=Dave&age=30。。。 要获取名称,您可以执行$\u get['name'],它将返回Dave。我认为更好的方法是: $selection = $_GET['selection']; if (empty($se

我试图使用GET url中的查询来确定页面的内容。 这就是我所拥有的(为了清晰起见,对句子进行了编辑):


有比
$SERVER['query\u string']
更好的解析查询字符串的方法,具体来说,您可以使用
$\u GET
访问特定参数。示例:www.Example.com?name=Dave&age=30。。。 要获取名称,您可以执行
$\u get['name']
,它将返回
Dave
。我认为更好的方法是:

$selection = $_GET['selection'];
if (empty($selection)) {
    header('start.html') ;
}

else {
     $vars = array(
          'republic'=>array('title'=>'Private', 'welcome'=> 'Our .', 'declaration'=>'', 'signoff' => 'The Replublic will strike back'),
          'rebels'=>array('title'=>'Comrade', 'welcome' => "Hey comrade, welcome to the Underground Network!", 'declaration'=>"You see,o's!",'signoff' => "Rebel!!"),
          'robots'=>array('title'=>'Bit', 'welcome'=>'Our data', 'declaration'=>'Knowlegge W', 'signoff'=>'Ed now')
      );

      list($title, $welcome, $declaration, $signoff) = $vars[$selection];
}

这来自比较值和类型的三元号
=
。你应该看看

我建议您只使用两个equals,顺便说一句,您可以使用$\u GET['selection']变量来简化代码:

<?php 
//decalre variables
$title ='';
$welcome = '';
$params = '';

$params = $_SERVER['QUERY_STRING'];
echo $_SERVER['QUERY_STRING'];
echo $params;

if (!isset($_GET['selection']) { // Check whether selection is set 
    header('start.html') ;
} else {
    if ($_GET['selection'] == "republic") {
        //echo $params;
        //echo 'Republic';
        $title = "Private";
        $welcome = "Our .";
        $signoff = "The Republic will strike back!";
    } 
    else if ($_GET['selection'] == "rebels")    {
        //echo $params;
        //echo 'Rebels';
        $title = "Comrade";
        $welcome = "Hey comrade, welcome to the Underground Network!";
        $declaration="You see,o's!";
        $signoff = "Rebel!!";
    } 
    else if ($_GET['selection'] == "robots"){
        //echo $params;
        //echo 'Robots';
        $title = "Bit";
        $welcome = "Our data ";
        $declaration="Knowledge w.";
        $signoff = "ed now.";
    }
    else {
        echo 'There was an error - please go back.';
    }
}

我相信第一个
if($params='')
应该是
==
。为什么不使用
$\u GET['selection']
?另外,使用if(空($variable))而不是if($variable='')要好得多。。。empty检测是否没有值或空格(0太多了,所以要小心整数),谢谢大家-“==”修复了它,并使用了is simplerand
$SERVER['QUERY\u STRING']
<?php 
//decalre variables
$title ='';
$welcome = '';
$params = '';

$params = $_SERVER['QUERY_STRING'];
echo $_SERVER['QUERY_STRING'];
echo $params;

if (!isset($_GET['selection']) { // Check whether selection is set 
    header('start.html') ;
} else {
    if ($_GET['selection'] == "republic") {
        //echo $params;
        //echo 'Republic';
        $title = "Private";
        $welcome = "Our .";
        $signoff = "The Republic will strike back!";
    } 
    else if ($_GET['selection'] == "rebels")    {
        //echo $params;
        //echo 'Rebels';
        $title = "Comrade";
        $welcome = "Hey comrade, welcome to the Underground Network!";
        $declaration="You see,o's!";
        $signoff = "Rebel!!";
    } 
    else if ($_GET['selection'] == "robots"){
        //echo $params;
        //echo 'Robots';
        $title = "Bit";
        $welcome = "Our data ";
        $declaration="Knowledge w.";
        $signoff = "ed now.";
    }
    else {
        echo 'There was an error - please go back.';
    }
}