How to set a global variable accessible throughout the application

前端 未结 7 1742
失恋的感觉
失恋的感觉 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 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

提交回复
热议问题