How to set a global variable accessible throughout the application

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

提交回复
热议问题