php 如何设置可在整个应用程序中访问的全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8952613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a global variable accessible throughout the application
提问by Amit Kumar Gupta
I developed a PHP page with global variable like this;
我用这样的全局变量开发了一个 PHP 页面;
global $amty_imgCache; $amty_imgCache = array();
$GLOBALS["amty_imgCache"]=$amty_imgCache;
This page has functions to add/delete entries to/from this array.
此页面具有向/从该数组添加/删除条目的功能。
I called a function on antother PHP page to display its count and to put some elements into this global array this way;
我在另一个 PHP 页面上调用了一个函数来显示它的计数并以这种方式将一些元素放入这个全局数组中;
Count <?php echo amty_getImageCacheCount(); ?>
<?php amty_putIntoImageCache(100,0); ?>
Count <?php echo amty_getImageCacheCount(); ?>
But on every refresh first it displays count 0 then 1.
但是在每次刷新时,它首先显示计数 0 然后是 1。
How can I persist values of global variable across entire application.
如何在整个应用程序中保留全局变量的值。
回答by Pushpendra Singh Thakur
Use APC or memcache to store such values. You can not only access these values from any page but can also access from any server.
使用 APC 或 memcache 来存储这些值。您不仅可以从任何页面访问这些值,还可以从任何服务器访问。
回答by Paul Bain
You cant really persist variables across the execution of pages without saving them to some persistent store.
如果不将变量保存到某个持久存储,您就无法在整个页面执行过程中真正持久化变量。
If you need to store a variable only for a specific user, use the session using session_start();
and then using $_SESSION;
如果您只需要为特定用户存储变量,请使用会话 usingsession_start();
然后使用$_SESSION;
If it's for the whole application, you should look into using a database or saving data to a file. If saving to a file, checkout serialize()
and unserialize()
which will let you store the state of your variables to a text representation.
如果是针对整个应用程序,您应该考虑使用数据库或将数据保存到文件中。如果保存到文件,请检出serialize()
,unserialize()
这将使您将变量的状态存储到文本表示中。
回答by rauschen
You got something wrong.
你有什么不对的。
All variables in php outside a function or class are global variables!
php 中函数或类之外的所有变量都是全局变量!
To use a global variable in a function and change its value use global
-Keyword in the function
要在函数中使用全局变量并更改其值global
,请在函数中使用-Keyword
$amty_imgCache = array();
$amty_imgCache[] ="my_first_img.png";
function amty_getImageCacheCount() {
global $amty_imgCache;
echo "count is:" ,count($amty_imgCache);
}
But this storage is only per one request. If you want to store things longer use a session
or a database
or a file
但是这种存储仅针对一个请求。如果你想存放更长时间的东西,请使用 asession
或 adatabase
或 afile
回答by a sad dude
PHP doesn't have any application-level persistence. You might want to look at Memcache for the quickest solution (if you can install it, of course).
PHP 没有任何应用程序级别的持久性。您可能想查看 Memcache 以获得最快的解决方案(当然,如果您可以安装它)。
回答by EdNdee
While I think most answers here are appropriate, I don't find them complete enough. PHP certainly has application-wide persistence only that you'd have to build such variables into PHP itself or a module that is loaded when PHP is first loaded by your web server. That means extending and rebuilding PHP itself or at least building and loading an external module.
虽然我认为这里的大多数答案都是合适的,但我认为它们还不够完整。PHP 当然具有应用程序范围的持久性,只是您必须将此类变量构建到 PHP 本身或 Web 服务器首次加载 PHP 时加载的模块中。这意味着扩展和重建 PHP 本身或至少构建和加载外部模块。
回答by Abhishek Dhanraj Shahdeo
You can make use of PHP sessions. The session variables are super globals and can be accessed anywhere until you destroy the session. You just need to mention the starting of a session by
您可以使用 PHP 会话。会话变量是超级全局变量,可以在任何地方访问,直到您销毁会话。您只需要通过以下方式提及会话的开始
<?php
session_start();
//...your code
$_SESSION['variable']=$variable;
//....your code
?>
On the page you would wanna set the variable and then you can make use of it on the same page as follows :
在您想要设置变量的页面上,然后您可以在同一页面上使用它,如下所示:
<?php
//.....your code
$variable=$_SESSION['variable'];
//....your code
//always remember the destroy the session after the use of it
session_destroy();
?>
回答by Vyktor
First of all, when you're using global variables in function you should use either global
or $GLOBALS
, not both. So it should look like this:
首先,当您在函数中使用全局变量时,您应该使用global
或$GLOBALS
,而不是两者都使用。所以它应该是这样的:
function amty_putIntoImageCache( $i, $j){
global $amty_imgCache;
$amty_imgCache[ $i] = $j;
}
The second, why aren't you using static class instead of global variable? The correct design for this would be static class usage, example:
第二,为什么不使用静态类而不是全局变量?正确的设计是静态类使用,例如:
class amty {
static protected $images = array();
static public function put( $i, $j){
self::$images[$i] = $j;
}
}
amty::put( 100,0);
And (I believe this is what you were asking about), when you want to use global variable in entire application on every page (that means after reloading) you should use:
并且(我相信这就是您要问的),当您想在每个页面的整个应用程序中使用全局变量时(这意味着在重新加载后),您应该使用:
session_start() // Be careful to use this just once
$_SESSION['variable'] = 'This value will persist as long as session lives';
Session exist per one user/one connection (php generates session id and stores it (by default) into cookies).
每个用户/一个连接都存在会话(php 生成会话 ID 并将其(默认)存储到 cookie 中)。
If you really need data to be accessible trough whole application you should use database or file storage.
如果您确实需要通过整个应用程序访问数据,您应该使用数据库或文件存储。