I am learning PHP from w3schools' PHP Tutorial.
While learning PHP I came across the concept of predefined global variables i.e. Superglobals.
In a curiosity to understand "Superglobals" more deeply I wrote the following code and executed it in a browser on my local machine(i.e.localhost) :
<!DOCTYPE html>
<html>
<body>
<?php
echo "<pre>";
print_r($GLOBALS);
echo "</pre>";
?>
</body>
</html>
I got following output in a browser :
Array
(
[_GET] => Array
(
)
[_POST] => Array
(
)
[_COOKIE] => Array
(
[toWorkNormally] => 1
)
[_FILES] => Array
(
)
[GLOBALS] => Array
*RECURSION*
)
The above output has created many doubts in my mind as follows :
As per my knowledge in PHP there are nine types of
superglobals (predefined PHP global variables) viz. $GLOBALS,
$_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE and
$_SESSION then my doubt is what does the array elements from the predefined global array
$GLOBALS viz. [_GET], [_POST], [_COOKIE], [_FILES] mean as they have
their own independent existence as superglobals?
What is meant by [toWorkNormally] => 1 from above array output?
What does mean by RECURSION in element [GLOBALS] and how to print
those elements?
As the purpose of $GLOBALS array is to store variables declared by user globally then how this array has been pre-populated with some other values as I haven't declared any global variable in my code?
Note : I'm using "Microsoft Windows 10 Home Single Language" operating system on my machine. It's a 64-bit Operating System. I'm using latest edition of XAMPP with PHP 7.0.13 and HTTP Apache web server v.2.4.23 for running the program locally. Also, please note that I have not defined any other variable as global or local in my code.
From my knowledge of PHP and doing some research as well as testing this on multiple OS' with various version of PHP I found the following.
Question 1 & 3:
Yes you are correct with regards to the 9 superglobals, but a very important thing to keep in mind is that $GLOBALS -- References all variables available in global scope.
An interesting sidenote, notice that $GLOBALS is the only superglobal that doesn't start with an underscore.
Because of the fact that $GLOBALS contains references to all the other superglobals including itself, when we print_r($GLOBALS) it will also include the other superglobals in the output. Because $GLOBALS references itself as well we get the RECURSION you asked about in your 3rd point. You can think of it as a infinite dimensional array containing $GLOBALS. Almost the same idea as an infinte loop.
[GLOBALS] => Array
(
[GLOBALS] => Array
(
[GLOBALS] => Array
(
...
)
)
)
Instead the script sees this and stop executing and just prints RECURSION. Now I have tested it on 3 different environments and each time the order in which the superglobals are printed changed, but as soon as it hits $GLOBALS it stops and prints RECURSION.
Question 2:
I could not find any info on $_COOKIE[toWorkNormally] => 1. I am assuming this is set somewhere else. I didn't see it in any of my tests.
Question 4:
This is neither correct nor incorrect. The purpose of $GLOBALS is not to store all variables created by the user globally. It merely references all variables available in global scope including, the superglobals. That is why you are seeing all the other superglobals in the output. But a lot of developers assume that the user defined global variables are stored in $GLOBALS.
Description in the PHP.net manual
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.
To view all the superglobals you will have to print_r() each one of them individually.
To check all user defined global variables you can use array_keys($GLOBALS) all the items which are not superglobals will most probably be user defined global variables.
EDIT in response to users comments
In response to your 1st comment, No they are not different. The superglobals not printed are still part of the array but execution/output stops because it hits the RECURSION when it gets to $GLOBALS. The superglobals are printed in a random order and which ever comes after the $GLOBALS will not be seen as it detects a RECURSION at $GLOBALS and stops the output.
You can check all the superglobals/global variables by using print_r(array_keys($GLOBALS)); With an exception of $_SESSION because a session has not been started yet. print_r($_SESSION) will give you an undefined variable $_SESSION Notice. You will be able to see $_SESSION when you put session_start(); just before you print it.
Link to What References Are in PHP
References in PHP are a means to access the same variable content by different names.
Note that in PHP, variable name and variable content are different, so the same content can have different names
The PHP manual says the following about the $GLOBALS variable:
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.
This describes exactly what the variable does. It is simply a reference to existing variables.
The RECURSION you are talking about is the $GLOBALS variable referring to itself. Since we don't want PHP to endlessly output the same output and crashing your server in the process, there is a built-in failsafe that shows you the RECURSION alert if this is the case.
I would like to add that $GLOBALS is a superglobal, or preset global, variable. This means that it is available in all scopes throughout your script.
Resources
The PHP manual about $GLOBALS
$GLOBALS is the global of all super global and user defined variables. So for example if you have declared variable $a = 10; in your $GLOBALS array you have key=>value pair where key is a and value is 10.If you want to get something from $GLOBALS you just need to write it as array key.
example
$a = 25;
echo $GLOBALS['a'];
In the example above output will be the value of $a so 25;
In your example toWorkNormally=>1 it`s mean that you have set cookie with name toWorkNormally and with value 1 or true
Also when you submit form with get or post method in the $GLOBALS['_GET'] or $GLOBALS['_POST'] there you can find your form data as you can get them from super global $_GET or $_POST
1. As per my knowledge in PHP there are nine types of superglobals (predefined PHP global variables) viz. $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE and $_SESSION then my doubt is what does the array elements from the predefined global array $GLOBALS viz. [_GET], [_POST], [_COOKIE], [_FILES] mean as they have their own independent existence as superglobals?
From PHP's doc:
References all variables available in global scope
This means you can access a superglobal directly or from $GLOBALS, yes, you have two ways of accessing them.
2. What is meant by [toWorkNormally] => 1 from above array output?
It's inside $_COOKIE so there's a cookie named toWorkNormally with the value of 1. More info on cookies
3. What does mean by RECURSION in element [GLOBALS] and how to print those elements?
Recursion means it refereces itself, if it was printed then it would show the contents of $GLOBALS again nested inside GLOBALS, that would cause infinite loop. To avoid that PHP just printed *RECURSION* instead.
4. As the purpose of $GLOBALS array is to store variables declared by user globally then how this array has been pre-populated with some other values as I haven't declared any global variable in my code?
From PHP's doc:
Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.
So in other words, $GLOBALS will show you those predefined variables from PHP and also the values you set manually.
Related
I found what appears to be an abusable bug in the PHP language. When creating variable variables a seemingly illegal variable can be declared and then handled as long as you keep accessing it as a variable variable.
Example:
${'0'} = 1; //I am an illegal variable called $0
${'0'}++; //I can be accessed and manipulated.
var_dump(${'0'}); //Output: int(2)
This behavior appears rather odd. It is briefly mentioned in the official documentation for SimpleXml as a way to create variable names that contain hyphens but my example shows it can be abused pretty badly nonetheless.
I would like to know how come this behavior is possible and is tolerated when the code is parsed?
Internally PHP stores variables (zend_array* symbol_table) in a same data structure that is used for arrays. This allows you to have variable names with the same constraints as array keys.
Eg. Zend API function zend_set_local_var sets a value to symbol table using zend_hash_update, which is a function used to manipulate PHP array types as well.
We can't however allow every character in variable names in PHP source code. That's because lexical analysis must distinguish labels from other tokens. Variable variables offer a workaround for this.
It's not a bug nor a way to abuse anything. That being said, the documentation states that all labels, including variables, must have a regex form [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*, so i wouldn't rely on having arbitrary characters in variable names.
What ${'0'} = 1; actually does, it sets value 1 to the current scope's symbol table at key 0.
You can get that particular table with get_defined_vars function.
Looking at the source code, we'll see that the function just copies the current symbol table and returns it to caller.
PHP extract function (src) actually checks that keys have a valid label format (by calling php_valid_var_name), so you can't generate variables with funky names using that.
Anyway, even though it's possible to create variables of any name using variable variable syntax (even a variable with no name ${''}), i think it's bad practice. Even worse if a library expects or enforces you to do so. Saying it's a workaround might be a bit overstatement actually. Maybe it should be considered as an implementation detail and an undocumented feature.
#Nadav I don't have full idea about that but have a little bit idea,
Remember that curly braces({}) literally mean "evaluate what's inside the curly braces" so, you can squeeze the variable creation like you did ${'0'} = 1
reffered it from this http://php.net/manual/en/language.variables.php#73373
PHP stores variable in the array form, if you want to check then use $GLOBALS(an array) is the super global variable which has all the reference of the variables present in the script, it stores variable name as key and value as variable value, suppose below case:
<?php
${'0'} = 1; // here php stores it as 0th index in the array
${'1'} = 2; // here php stores it as 1th index in the array
$b = "I am b"; // here php stores it as bth index in the array
echo "<pre>";
print_r($GLOBALS);
output:
Array
(
[_GET] => Array
(
)
[_POST] => Array
(
)
[_COOKIE] => Array
(
)
[_FILES] => Array
(
)
[GLOBALS] => Array
*RECURSION*
[0] => 1
[1] => 2
[b] => I am b
)
PHP has list of reserved variables, visit here.
If i use one of the reserved keyword as my variable, it is working for me.
<?php
$_GET = 10;
echo $_GET;//10
?>
Please correct me, if my understanding is wrong?
"Predefined" is not the same as "reserved". PHP gives these variables default values, but you can still use their names for your own purposes. But you shouldn't, since it's poor style.
If you pass GET data into that file like http://example.com/example.php?id=1
Then it will conflict with your $_GET variable..
$_GET = Array { [id] => 1 }
After your declaration the value will be changed as
$_GET = 10
It will overwrite the old value..
I can't imagine a situation where you would want to do what you're suggesting.
in the case of $_GET, $_SESSION, they are arrays where you can assign a key value pair at will, that's what they're designed for.
$_SESSION["UID"]=username;
for example
$_GET is not a reserved variable.
$_GET is an associative array of variables passed to the current script via the URL parameters. Also is a superglobal. Read the documentation to learn how to use it
You should use like
$_GET['key']=10;//good
Doing
$_GET =10;//bad
will overwrite the value and it works but is a very wrong way to use a superglobal and they were not designed to work that way
Is it possible to list out all the Global Variable. Such , as to print all session variable we can use print_r($_SESSION); as the same way, if i want to know how many Global variables are define in my site. is it possible? Please Help.
I need the list which can show me all the Global variables List in PHP.
Yes, PHP $GLOBALS array will show you.
<?php
echo "<pre>";
print_r($GLOBALS);
echo "</pre>";
To get names of all global variables do this:
array_keys($GLOBALS)
More docs:
on array_keys(): http://php.net/manual/en/function.array-keys.php
on $GLOBALS: http://php.net/manual/en/reserved.variables.globals.php
$GLOBALS — References all variables available in global scope also check this PHP global or $GLOBALS here you find many important and useful thing about $GLOBALS
The reserved $GLOBALS variable is an array containing all global variables.
In the array, the keys are variable names and the values are variable values.
Well, there does exist a $GLOBALS variable in PHP.
But you can't do a var_export() on it. It'll lead to an error like: Nesting level too deep - recursive dependency?
When I print $GLOBALS using this code:
<?php print_r($GLOBALS); ?>
I get this output:
Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) [GLOBALS] => Array *RECURSION* )
What does "*RECURSION*" mean in this case, and why are $_SERVER, $_REQUEST, etc. not printed as well?
See this part of PHP Manual:
Keep in mind that $GLOBALS is, itself, a global variable. So code like this won't work:
<?php
print '$GLOBALS = ' . var_export($GLOBALS, true) . "\n";
?>
This results in the error message: "Nesting level too deep - recursive dependency?"
You already have retrieved the whole list - you just cannot display part of it (the one containing a recursion, because you would have timeout rather than anything meaningful).
When it comes to $_REQUEST, it is a derivative from $_GET, $_POST and $_COOKIE, so its content is redundant.
EDIT: There is an old bug / feature, that seems to be populating $GLOBALS with $_SERVER and $_REQUEST when they are accessed. So try to access $_REQUEST and hope it helps. Anyway, it can be found in $GLOBALS after that: ideone.com/CGetH
$GLOBALS contains itself as an array. In the PHP reference you can find the definition of $GLOBALS:
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.
Therefore, it has to contain also itself, which leads to a recursion.
The other arrays are probably just empty, since nothing else has happened in your script.
There is an old joke about recursion: "To understand recursion, you must understand recursion".
BTW: It outputs _SERVER on my computer.
Is there a function and/or object and/or extension in PHP that will let you view all the variables defined in the current scope? Something like:
var_export($GLOBALS)
but only showing variables in the current symbol table.
get_defined_vars
This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
get_defined_vars() does exactly what you want.
This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
>>> function test($foo) { print_r(get_defined_vars()); }
>>> test('bar');
Array
(
[foo] => bar
)