PHP如果isset $ _GET任何参数,如果它们出现在对脚本的POST请求中

问题描述 投票:-1回答:4

我有一个奇怪的例子,脚本应该不时地接收2个参数的值。其中一个参数是geo,另一个是tids,我必须将tid分解为4个不同的变量供我使用。我想设置一个条件,其中geo参数可能始终存在于请求的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的POST,如果不是 - 像往常一样处理单独的t 1,2,3和4,并且geo位置以不同的方式发送给其他参数。如果我尝试用逗号(,)或OR运算符(||)分隔它们,这意味着必须存在所有条件,即将tids和geo始终发送到脚本,否则脚本将不会处理geo参数已设置。如果geo参数存在但tid可能不存在,如何设置它来处理?事情是,其他一些脚本可以在不同的参数tid1,tid2,tid3和tid中分别传递tid1,2,3,4等的数据,并且geo在内部用其他变量提取,并且从其他一些来源我得到所有的tids与分隔符混合在一起作为一个字符串,位置作为参数geo。这就是为什么我想区分同一脚本中的案例而不必复制具有不同名称的相同脚本来区分案例并且让单独的API调用不同的文件,因为它们必须以不同方式处理数据。

php if-statement parameters get isset
4个回答
0
投票

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

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
}

0
投票

您可以使用嵌套的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
}

希望这可以帮助。


0
投票
 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的值


0
投票

我做了一些广泛的测试,看起来像我写的代码实际上做了工作,错误是在其他地方。感谢大家投入时间和精力帮助我回答!非常感激。

© www.soinside.com 2019 - 2024. All rights reserved.