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)

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)