5

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.

Amit Kumar Gupta
  • 6,271
  • 11
  • 54
  • 77
  • I dont wanna put it into session since session variables should be set when they have user level scope not application level. – Amit Kumar Gupta Jan 21 '12 at 11:32
  • "Global" variables in PHP [aren't really](http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why/1557799#1557799) application-persistent. They just exist for one invocation. Use sessions. – mario Jan 21 '12 at 11:33
  • 1
    If that disappoints you, you probably did not familiarize enough with the language before choosing it. It's well known that PHP is Shared-Nothing. It's one of the reasons why it's easy to scale. – Gordon Jan 21 '12 at 12:06
  • Thanks @Gordon, I am a java developer and does PHP code part time, just for my hobby, googling its syntax and concept. – Amit Kumar Gupta Jan 21 '12 at 12:45
  • @articlestack: You're looking for an application server for PHP, like https://github.com/indeyets/appserver-in-php/wiki – hakre Jan 21 '12 at 12:52
  • This is a good question (and quite hard to google up the answer, as in, not a matter of mere seconds). I don't uderstand why the downvotes. – siledh Oct 07 '13 at 12:02

7 Answers7

5

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.

3

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;

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.

Paul Bain
  • 4,290
  • 1
  • 14
  • 30
1

You got something wrong.

All variables in php outside a function or class are global variables!

To use a global variable in a function and change its value use global-Keyword in the function

$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

rauschen
  • 3,726
  • 2
  • 11
  • 13
1

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).

a sad dude
  • 2,766
  • 15
  • 19
0

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.

EdNdee
  • 715
  • 1
  • 9
  • 18
0

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:

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).

If you really need data to be accessible trough whole application you should use database or file storage.

Vyktor
  • 19,006
  • 5
  • 53
  • 93
  • I am using in the same way as you suggested in 1st snippet. I guess making it static will also not resolve my problem. session is not preferable. I am going for database. – Amit Kumar Gupta Jan 21 '12 at 11:46
  • @articlestack There's answer with `serialize` and `unserialize`, I think you should go with it than or use database common database approach and loading :) – Vyktor Jan 21 '12 at 11:52
-1

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
    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();
    ?>
Community
  • 1
  • 1
Abhishek Dhanraj Shahdeo
  • 1,244
  • 2
  • 12
  • 35