比较PHP中的URL字符串

时间:2013-04-15 08:03:14

标签: php string comparison

我正在尝试使用来自GET网址的查询来确定网页的内容。 这就是我所拥有的(为清晰起见而编辑的句子):

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

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

if ($params='') {
    header('start.html') ;
} else {
    if ($params === "selection=republic") {
        //echo $params;
        //echo 'Republic';
        $title = "Private";
        $welcome = "Our .";
        $signoff = "The Republic will strike back!";
    } 
    else if ($params === "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 ($params === "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.';
    }
}

第一个回显显示正确的URL,但比较卡在第三个选项上。 救命啊!

2 个答案:

答案 0 :(得分:1)

解析查询字符串的方法比$SERVER['QUERY_STRING']更好,特别是您可以使用$_GET来访问特定参数。示例:www.example.com?name = Dave&amp; ofage = 30 ... 要获取名称,您可以执行$_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];
}

答案 1 :(得分:1)

这来自三重=符号,用于比较值和类型。你应该看到the difference here

我建议您只使用两个等号,顺便说一句,您可以使用$ _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.';
    }
}