Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between GLOBALS and GLOBAL?

Tags:

php

global

In PHP I want to know the differences between the GLOBAL and GLOBALS.

Some example:

print_r($GLOBALS);
like image 951
balaphp Avatar asked Nov 07 '11 10:11

balaphp


People also ask

What is the difference between global and GLOBALS in Python?

Global symbol table stores all information related to the global scope of the program, and is accessed in Python using globals() method. The functions, variables which are not associated with any class or function are stored in global scope.

What is the difference between global and super global variable?

Superglobal Variables in PHP are predefined global variables. Global variables are variables with global scope, which means that they can be used wherever needed – they do not need to be declared, nor do they need to be marked with global in functions.

What is a global in coding?

A global variable is a programming language construct, a variable type that is declared outside any function and is accessible to all functions throughout the program.

What is the difference between the $_ GET and $_ POST Super global variables?

Difference is: $_GET retrieves variables from the querystring, or your URL.> $_POST retrieves variables from a POST method, such as (generally) forms.


1 Answers

That are two different things related to the same: global variables.

$GLOBALS - PHP superglobal array representing the global variable table accessible as an array. Because it's a superglobal, it's available everywhere.

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

global - Keyword to import a specific global variable into the local variable table.


Then you asked:

But why we cant access the session and cookie variables by using $GLOBALS?

That's wrong, you can access session and cookie variables by using $GLOBALS:

$GLOBALS['_SESSION']['session_variable_name']

However $_SESSION is a superglobal as well, so you don't need to use either $GLOBALS nor global to access session variables from everywhere:

$_SESSION['session_variable_name']

Same applies to $_COOKIE.

like image 166
hakre Avatar answered Nov 02 '22 13:11

hakre