What is the difference between session variables and global variables in PHP?
-
try to study in http://php.net/manual/en/index.php site, that have complete manual for php. – Feb 13 '13 at 07:03
-
Please ask specific programming related question here. Read http://stackoverflow.com/faq – Rikesh Feb 13 '13 at 07:05
-
when the global variables get destroyed? – Srividhya Feb 13 '13 at 07:11
5 Answers
Global variables are variables that can be accessed from anywhere in the application, as they have global scope.
Session variables are can also be accessed from anywhere in the application, but they are different for different users, since they depend on the session. They die when a particular user session ends.

- 4,503
- 4
- 21
- 42

- 4,634
- 2
- 25
- 32
-
-
Session variables die when the session ends.. But In case of Global varibles when it will die ? – Srividhya Feb 13 '13 at 07:14
-
2global variable die when you stop the application server or may be when they are killed forcibly – Smita Feb 13 '13 at 07:17
-
1Is it that the global variables are shared across different users.? – Veer Shrivastav Jul 03 '14 at 18:34
-
2Global variables are not common for the whole application: they don't persist across different pages (they're destroyed every page change like any other variable that's not in the session). Plus, they're not shared between users. – T30 Mar 30 '17 at 10:14
-
global
is just a keyword to access a variable that is declared in the top level scope and isn't available in the actual scope. This hasn't anything to do with the session: do not persist between pages.
$a = "test";
function useGlobalVar(){
echo $a; // prints nothing, $a is not availabe in this scope
global $a;
echo $a; // prints "test"
}
$GLOBALS
is another way to access top-level scope variables without using the global
keyword:
$a = "test";
function useGlobalVar(){
echo $GLOBAL['a']; // prints "test"
}
There's a bit of of confusion between global
and superglobals
: Superglobals (like $GLOBALS, $_REQUEST, $_SERVER) are available in any scope without you having to make a global declaration. Again, they do not persist between pages (with the exception of $_SESSION).
$_SESSION is a Superglobal array that persist across different pages.

- 3,389
- 2
- 24
- 21

- 11,422
- 7
- 53
- 57
-
1Weel explained in [this article](http://www.wellho.net/mouth/936_Global-Superglobal-Session-variables-scope-and-persistance-in-PHP.html). – T30 Mar 30 '17 at 10:04
Session variables are variables stored server side that persist for a given client connection.
global variables are variables that have a universal (global...) scope in your php code. these variables are not necessarily dependent on a given client connection
for sessions see: http://www.php.net/manual/en/book.session.php
for global varialbes see: http://www.tutorialspoint.com/php/php_global_variables.htm
lastly, this type of question isn't the most appropriate for this forum, see: https://stackoverflow.com/faq#dontask
"You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page.
Your questions should be reasonably scoped. If you can imagine an entire book that answers your question, you’re asking too much.
If your motivation for asking the question is “I would like to participate in a discussion about ______”, then you should not be asking here. However, if your motivation is “I would like others to explain ______ to me”, then you are probably OK. (Discussions are of course welcome in our real time web chat.)"
Global variables are any variable that's declared outside of any function or class scope and are used inside another function by using the global
keyword, e.g.
$a = 123; // this is a global variable
function foo()
{
global $a; // and this is the explicit declaration
}
Super globals are like regular globals, except that they're implicitly declared global within functions so that they're always available.
Lastly, session variables are accessible via the super global $_SESSION
and are perpetuated by sending and accepting a session identifier.

- 170,779
- 38
- 263
- 309
global variables are those variables that are accessible inside your all php file and php defines some of the global variables which are available to all php scripts. Ex - $_POST , $_SESSION , $_REQUEST .
global is also a keyword which is used when you want to access a variable defined outside the function.
<?php
$name = "xyz" ;
function hello(){
global $name ;
echo $name ;
}
?>

- 121
- 1
- 4