关于$_SESSION的用法

BlackBone 2004-05-13 02:49:44
1. 直接在代码里使用$_SESSION['count']
$_SESSION['count']++;
2. 使用变量
$count = $_SESSION['count];
...
$count++;
...
$_SESSION['count'] = $count;

那种方式更可取?
...全文
121 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlackBone 2004-05-13
  • 打赏
  • 举报
回复
session的内容是否会在页面打开时加载到内存里?

如果是这样的话,那第一种方式应该更好些
ccterran 2004-05-13
  • 打赏
  • 举报
回复
都可取。但前者看起来更简便一些。
BlackBone 2004-05-13
  • 打赏
  • 举报
回复
在http://www.php.net/manual/zh/ref.session.php有这么一段:

A small warning! $_SESSION can be slooowwwww....

I've never heard of this, but it turns out $_SESSION is much slower than any regular array, even an exact copy of $_SESSION. Copying large amounts of data in/out of $_SESSION is seriously slow and each access to $_SESSION is noticeably slower than regular arrays.

The lesson I learned? Don't use $_SESSION in loops that run very much. Even copying data from $_SESSION before the loop won't help a lot if there's a lot of data there due to a delay that can be pretty hefty, almost equal to working directly on $_SESSION with foreach() and actually slower than working directly on $_SESSION if you need to put the data back in the array in my experience. It's better to pass the data another way if possible, i.e. save the SQL query and re-run, store in a database, etc.

Just a warning for those who may be using this array in medium to large loops or trying to pass lots of data. I hope it saves you the hours of optimizing I've had to spend!

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧