如何检查我当前 url 的查询字符串中的特定键值对?

How to check the query string of my current url for a specific key-value pair?

我如何在 PHP 中识别我当前的 URL 是否包含此文本 special_offer=12。 例如,如果我的域 URL 是 http://www.example.com/newproducts.html?special_offer=12 则 echo something.

或者,域可以是 http://example.com/all-products.html?at=93&special_offer=12

此文本将始终相同:special_offer=12

if (isset($_GET['special_offer'] {
   do something...
}

var_dump($_GET); 查看超全局 $_GET 的工作原理

您可以使用 $_GET 超全局访问查询参数,在这种情况下:

if(isset($_GET['special_offer']) && $_GET['special_offer'] == 12){ 
  echo "Something";
}

只需检查 $_GET var ?

if(!empty($_GET["special_offer"]) && $_GET["special_offer"] ==12 ){
    //You're code here
}

You Can Access To Query Parameter Using Get Like This:

if(isset($_GET['special_offer'] == 12)){ 

  //echo " ECHO YOUR CODE HERE ";

}

注意:避免使用 isset() 和使用 !empty() ..因为 isset() 也接受 null 值来存储,所以避免使用它!!

 if(!empty($_GET['special_offer']) && $_GET['special_offer'] == 12){ 
    
      //echo " ECHO YOUR CODE HERE ";
    
    }

NOTE:

Empty 检查是否设置了变量,如果设置了则检查它是否为 null、""、0 等

1 Isset just checks if is it set, it could be anything not null

2 With empty, the following things are considered empty:

  • ""(空字符串)
  • 0(0 为整数)
  • 0.0(0 作为浮点数)
  • "0"(0 作为字符串)
  • 错误
  • array()(空数组)
  • var $var; (声明的变量,但在 class 中没有值)

参考:http://php.net/manual/en/function.empty.php