Global variable

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.

Is this possible to get the value in PHP?..

Thanks in advance

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

Method of passing a value depends on variable’s purpose.
Query string is often used.

Thanks for your reply.

Those pages are separate

I get the values using OOPs concepts.

That means one common class file that having SET and GET function for the variable.

in INDEX file i set the value of the variable
in INDEX1 file i get the STORED value of the variable.

with out using SESSION

Is this Possible to make in PHP?..

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.

Depends on how you set and get variables. But you can still use cookies to set the value in index1.php and get it in index1.php.