【问题标题】:PHP if isset $_GET any of the parameters if they are present in the POST request to a scriptPHP if isset $_GET 任何参数,如果它们出现在对脚本的 POST 请求中
【发布时间】:2018-09-18 12:06:19
【问题描述】:

我有一个奇怪的情况,脚本应该不时地接收 2 个参数的值。其中一个参数是 geo,另一个是 tids,我必须将 tids 分解为 4 个不同的变量供我使用。我想设置一个条件,其中地理参数可能始终存在于请求的 url 中,而 tid 可能不存在。使用 OR 运算符“||”可以实现吗?现在我有

if (isset($_GET['tids']) || isset($_GET['geo'])) { 
    $tidsexplode=$_GET['tids'];
    $pieces=explode("separator", $tidsexplode);
    $country=$_GET['geo'];
    // do what I want with tids exploded $pieces and geo
}else{
    // take action as with tid1, tid2, tid3, tid4 and geo location being sent separately in different parameters and their other variables as usual
}

基本上我想告诉脚本以我的方式行事,如果它是用参数 tids 和 geo 发布的,如果不是 - 像往常一样处理单独的 tids 1、2、3 和 4,并且地理位置以不同的方式发送方式与其他参数。如果我尝试用逗号 (,) 或 OR 运算符 (||) 分隔它们,这意味着所有条件都必须存在,即始终将 tids 和 geo 发送到脚本,否则脚本不会处理地理参数已设置。如果地理参数存在但 tids 可能不存在,如何将其设置为处理?问题是其他一些脚本可能会在不同的参数 tid1、tid2、tid3 和 tid 中分别传递 tid1、2、3、4 等的数据,并且 geo 是在内部与其他变量一起提取的,并且从其他一些来源我得到了所有tids 与分隔符混合在一起作为一个字符串和位置作为参数 geo。这就是为什么我想区分同一脚本中的案例,而不必复制具有不同名称的同一脚本来区分案例,并让单独的 API 调用不同的文件,因为它们必须以不同的方式处理数据。

【问题讨论】:

标签: php if-statement parameters get isset


【解决方案1】:

我想你想说的是,如果我两个都这样做,如果我只有一个,那么另一个呢?如果是这样,应该这样做,或者至少为您指明正确的方向。

if (isset($_GET['tids']) && isset($_GET['geo'])) { 
     $tidsexplode=$_GET['tids'];
     $pieces=explode("separator", $tidsexplode);
     $country=$_GET['geo'];
     // do what I want with tids exploded $pieces and geo
}elseif (isset($_GET['tids']) && !isset($_GET['geo'])){
    // take action as with tid1, tid2, tid3, tid4 and geo location being sent separately in different parameters and their other variables as usual
}

【讨论】:

    【解决方案2】:

    您可以使用嵌套 IF 语句。试试下面的代码

    if (isset($_GET['geo'])) {        
       $country=$_GET['geo'];
       if( isset($_GET['tids']) ){
          $tidsexplode=$_GET['tids'];
          $pieces=explode("separator", $tidsexplode);
       }      
    }else{
      // take action as with tid1, tid2, tid3, tid4 and geo location being sent separately in different parameters and their other variables as usual
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:
       if (isset($_GET['tids']) || isset($_GET['geo']))
       {
         //run process for both
       }
       else 
       {
         function tids ($tid1=null, $tid2=null, $tid3=null, $tid4=null){/*run process*/}
      
        //split tids
        tids($tid1,$tid2,$tid3,$tid4); //call tids function
       }
      

      如果不可用,上述函数会将$tids 的值替换为null

      【讨论】:

      • 我可以试一试。谢谢!
      【解决方案4】:

      我进行了一些广泛的测试,看起来我编写的代码确实可以完成这项工作,而错误出现在其他地方。感谢大家投入时间和精力来帮助我回答!非常感谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-19
        • 2011-06-13
        • 2015-03-05
        • 1970-01-01
        • 2017-04-10
        相关资源
        最近更新 更多