在 PHP 中跨脚本读取和写入全局变量
PHP 是否有全局变量可以被一个正在运行的脚本修改并被另一个脚本读取?
Does PHP have global variables that can be modified by one running script and read by another?
推荐答案
不,按照设计 PHP 是一种无共享"架构,这意味着在同时运行的进程之间或在一个接一个运行的请求之间不共享任何内容.有多种方法可以共享数据,但您必须明确地进行共享.
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.
如果您只想在来自同一用户的 2 个请求之间共享,会话或 cookie 可能是您要走的路.
If you just want to share between 2 requests from the same user, sessions or cookies might be the way to go.
如果您想在多个用户之间共享,您可能需要某种共享持久性,要么是短期缓存(例如 memcached),要么像数据库一样更健壮.
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.
相关文章