dongza6247 2015-04-01 12:37
浏览 18
已采纳

我可以使用以下内容取消设置几个会话

I have several sessions like this:

$_SESSION['search']['max']
$_SESSION['search']['order']

Currently, I unset each one of them with a separate statement:

if(isset($_SESSION['search']['max']) && $_SESSION['search']['max'] != ''){
        unset($_SESSION['search']['max']);
    }
if(isset($_SESSION['search']['order']) && $_SESSION['search']['order'] != ''){
        unset($_SESSION['search']['order']);
    }

My question is - can I use the below general unset statement instead of having a separate statement for each session:

if(isset($_SESSION['search']) && $_SESSION['search'] != ''){
            unset($_SESSION['search']);
        }

Are there any advantages of declaring each unset statement separately?

Note: I need to unset them all at the same time, meaning I do not need to keep one or more to use again, therefore I believe one statement should do but need to be sure I am not missing out anything.

Thank you all!

  • 写回答

1条回答 默认 最新

  • dourao1968 2015-04-01 12:40
    关注

    I have several sessions like this:

    Nope. You have one session. Those are just variables.

    My question is - can I use the below general unset statement instead of having a separate statement for each session variable:

      if(isset($_SESSION['search']) && $_SESSION['search'] != ''){
            unset($_SESSION['search']);
        }
    

    You can. It's just normal array. And unset($a) is the same as $a = null, so your code is just:

    if(isset($_SESSION['search']) && $_SESSION['search'] != ''){
        $_SESSION['search'] = null;
    }
    

    Meaning that every element of $_SESSION['search'] array will be discarded.

    HOWEVER you are not unsetting session!
    Your session is still live, you just discard some of variables inside the session!

    Are there any advantages of declaring each unset statement separately?

    There is. You have control of which variables stay (like, "I want A and B to stay, but unset C and D"). But if you are like "nah, I'm sure I don't need any of them", then you can just unset them in a bulk and there is nothing wrong with that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测