dougang1965 2015-12-02 23:02
浏览 31
已采纳

在CodeIgniter 3.x中使用empty()和isset()检查会话变量

With CodeIgniter 3.0.3, the documentation states that session data can be retrieved a few different ways, including $_SESSION['item'] and $this->session->item. It's true. No matter how I set the session variable, I see that I can echo the value both ways.

Now, with that session variable populated, I can check for empty condition like this: if(empty($_SESSION['item'])), and it evaluates as bool(false) just as I would expect. However, if I check the same session variable in this manner: if(empty($this->session->item)) it evaluates as bool(true), which is NOT what I expect (since I can echo its value on the very next line). Why are these two different ways of checking for empty condition being evaluated differently?

  • 写回答

1条回答 默认 最新

  • douque8861 2015-12-02 23:27
    关注

    This is due to how empty() works. Compare this quote:

    The result for empty($registry->notEmpty) is a bit unexpeced as the value is obviously set and non-empty. This is due to the fact that the empty() function uses __isset() magic functin in these cases. Although it's noted in the documentation above, I think it's worth mentioning in more detail as the behaviour is not straightforward. In order to achieve desired (expexted?) results, you need to add __isset() magic function to your class:

    (taken from php.net: http://php.net/manual/en/function.empty.php, comment from user Janci)

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料