【问题标题】:How to check the query string of my current url for a specific key-value pair?如何检查我当前 url 的查询字符串是否有特定的键值对?
【发布时间】:2017-07-02 15:52:26
【问题描述】:

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

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

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

【问题讨论】:

  • 使用!empty() .. 检查special_offer 参数var 是否为== 12 ., bcoz isset() 也接受空值,所以在这种情况下isset() 失败!!

标签: php if-statement url query-string isset


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    if (isset($_GET['special_offer'] {
       do something...
    }
    

    var_dump($_GET); 看看超全局 $_GET 是如何工作的

    【讨论】:

      【解决方案3】:

      只需检查 $_GET 变量?

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

      【讨论】:

      • if 是小写,$_GET 是大写!
      • 我在使用智能手机:)。
      【解决方案4】:

      您可以像这样使用Get访问查询参数:

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

      注意:

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

      1 Isset 只是检查它是否设置,它可以是任何不为空的东西

      2 使用empty,以下内容被认为是空的:

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

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

      【讨论】:

      • 您好,我检查了您的代码,我遇到了这个致命错误:不能在表达式的结果上使用 isset()(您可以改用“null !== expression”)
      • 你可以使用!empty()然后
      • @Robert .. 你可以使用!empty()
      猜你喜欢
      • 2012-09-30
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 2018-09-28
      • 2016-11-20
      相关资源
      最近更新 更多