Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 7413

How to set a global variable accessible throughout the application

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.

Upvotes: 6

Views: 48586

Answers (7)

Abhishek Dhanraj Shahdeo
Abhishek Dhanraj Shahdeo

Reputation: 1356

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();
    ?>

Upvotes: -1

EdNdee
EdNdee

Reputation: 755

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.

Upvotes: 0

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.

Upvotes: 5

a sad dude
a sad dude

Reputation: 2825

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

Upvotes: 1

Vyktor
Vyktor

Reputation: 21007

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.

Upvotes: 0

rauschen
rauschen

Reputation: 3996

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

Upvotes: 1

Paul Bain
Paul Bain

Reputation: 4397

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.

Upvotes: 3

Related Questions