Hi Friends,
I am new to PHP.I am having some small clarification whether we use a Global variable for accessing one variable value into another PHP file.
Example :
I will assign the Global variable value in index.php file. I want to get the stored value into index1.php.
Does index.php include the index1.php in its code?
Or are these separate pages of a website?
If it’s included, you don’t need a global variable; the effect of include() is the same as copying and pasting the contents of the included file at that point. Its code has full access to any variables in scope at that point.
If it’s a separate webpage, then you can’t pass variables from one page to another by making the variable global. Each request is a totally separate execution of your code, with no shared state between them. You should use sessions to pass the variable between requests.
What do you mean by ‘global variables’? There are some variables in php that are called as superglobals like $_POST, $_GET, $_SESSIONS, $_COOKIE, etc which are used for different purposes. AFAIK, those variables are called global because of their scope in the script. Some are stored in the memory so are available only for a script and some work with client’s browser for example cookies and some work server like sessions. So if you want to store some values in a page and want to access to another, either you need to use cookie or sessions as per your requirement. If the data are confidential then better to use sessions ($_SESSIONS superglobal) and if the data are not that much confidential then you can simply store them in cookies ($_COOKIE superglobal).
Any objects you instantiate in one request do not exist in any other request. PHP is stateless. Every request starts with a brand new environment, populated only with any POST/GET/COOKIE data sent in the one HTTP request.