How to set a global variable accessible throughout the application

前端 未结 7 1673
失恋的感觉
失恋的感觉 2021-01-13 04:20

I developed a PHP page with global variable like this;

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


        
相关标签:
7条回答
  • 2021-01-13 04:54

    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.

    0 讨论(0)
  • 2021-01-13 04:58

    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.

    0 讨论(0)
  • 2021-01-13 05:02

    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

    0 讨论(0)
  • 2021-01-13 05:03

    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();
        ?>
    
    0 讨论(0)
  • 2021-01-13 05:15

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

    0 讨论(0)
  • 2021-01-13 05:16

    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.

    0 讨论(0)
提交回复
热议问题