如何在整个应用程序中设置一个全局变量 [英] How to set a global variable accessible throughout the application

查看:110
本文介绍了如何在整个应用程序中设置一个全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这样的全局变量开发了一个PHP页面;

  global $ amty_imgCache; $ amty_imgCache = array(); 
$ GLOBALS [amty_imgCache] = $ amty_imgCache;

此页面具有向该数组添加/删除条目的功能。



我在antother PHP页面上调用一个函数来显示它的计数,并用这种方法将一些元素放到这个全局数组中;

  Count<?php echo amty_getImageCacheCount(); ?> 
<?php amty_putIntoImageCache(100,0); ?>
计数<?php echo amty_getImageCacheCount(); ?>

但每次刷新时,它都会显示0和1的数字。

如何在整个应用程序中保存全局变量的值。

使用APC或memcache来存储这样的数据值。您不仅可以从任何页面访问这些值,还可以从任何服务器访问。


I developed a PHP page with global variable like this;

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;

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.

How can I persist values of global variable across entire application.

解决方案

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.

这篇关于如何在整个应用程序中设置一个全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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