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

查看:31
本文介绍了在 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.

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

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