在PHP中跨脚本读取和写入全局变量 [英] Reading and writing global variables across scripts in PHP

查看:530
本文介绍了在PHP中跨脚本读取和写入全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP是否有全局变量可以被一个运行脚本修改并被另一个脚本读取?解析方案

PHP是一种无共享体系结构,这意味着在同时运行的进程或者一个接一个地运行的进程之间不共享任何内容。有许多方法可以共享数据,但你必须明确地做到这一点。



如果你只是想分享来自同一用户的两个请求,会话或cookies可能是



如果您想要在多个用户之间共享,您可能需要某种共享持久性,可以是缓存中的短期内容(例如,memcached)或更多像数据库一样稳健。



无论哪种方式,数据实际上都是在每个请求中检索和重构的。它只是在会话中自动处理。


Does PHP have global variables that can be modified by one running script and read by another?

解决方案

No, by design PHP is a "share nothing" architecture, which means nothing is shared between processes running at the same time or between requests running one after another. There are ways to share data, but you have to do it explicitly.

If you just want to share between 2 requests from the same user, sessions or cookies might be the way to go.

If you want to share between multiple users, you probably want some sort of shared persistence, either short term in a cache (eg. memcached) or more robust like a database.

Either way, the data is actually being retrieved and reconstructed on each request. It's just handled automatically for you in the case of sessions.

这篇关于在PHP中跨脚本读取和写入全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆