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
)
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
)
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.
I know that prepending a '&' to your PHP variable sets up a reference to the original variable instead of copying its value like so:
$original = 'apples';
$secondary = &$original;
$original = 'oranges';
echo $secondary; // 'oranges'
If it works this way, why not just use the original variable then?
Passing by reference is useful and necessary when passing a variable as a parameter to a function, expecting that variable to be modified without a copy being created in memory. Many of PHP's native array_*() functions operate on array references, for example.
This function, for example, receives an array reference and appends an element onto the original array. If this was done without the & reference, a new array copy would be created in scope of the function. It would then have to be returned and reassigned to be used.
function add_to_an_array(&$array)
{
// Append a value to the array
$array[] = 'another value';
}
$array = array('one', 'two', 'three');
add_to_an_array($array);
print_r($array);
Array
(
[0] => one
[1] => two
[2] => three
[3] => another value
)
$original = 'apples';
function foo($word) {
$word = 'oranges';
}
foo($original);
echo $original; // apples, because only local $word was changed, not $original.
foo(&$original);
echo $original; // oranges, because $original and $word are the same
Pass by reference is really a cop out and goes against good encapsulation. If you need to manipulate a variable in that way, it probably should belong to a class as a member variable and then does not need to be passed to the function. Good OO design would usually make member variables immutable with a "final" keyword, but PHP doesn't have this. It's not intuitive that passing a variable to a function might change it's value which is why it should be avoided in most cases.
Also going to a more full OO design prevents you have having method signatures that are long and complex with many optional parameters that are difficult to re-factor.
A more interesting use of the is when it's used in the formal argument to a function
foo($a);
...
function foo (&$a) {
....
}
this allows you to modify a in the function.
There are many uses for references.
You can pass a reference to a variable to a function so you can change the value inside the function
You can use references to create linked lists
etc...
Just keep in mind that they're there, and you'll surely find an application for them when the time comes and you face a problem that can be solved with references.
Check out the following article for other ideas and uses of references:
http://www.elated.com/articles/php-references/
is there any way to get list of variables that were defined in function, or list of all accessible variables in a function?
There is 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.
it has to be called within each function you want to analyze, though.
http://php.net/manual/en/function.get-defined-vars.php
Documentation indeed ^^
func_num_args — Returns the number of arguments passed to the function
func_get_arg — Return an item from the argument list
func_get_args — Returns an array comprising a function's argument list
if the title seems too vague..
uhm i wanted to display every variable that i used to generate a page along with their variable names and values, is it possible and how?
foreach($_SESSION as $varname => $value) {
print "<b>".$varname."</b> = $value <br/>";
}
^the above sample is what i use to display all session variables, what if i need to display the variables i set to display the page? are they registered also in some form of an array or should i also echo them individually?
You can use get_defined_vars() which will give you an array of all variables declared in the scope that the function is called including globals like $_SESSION and $_GET. I would suggest printing it like so:
echo '<pre>' . print_r(get_defined_vars(), true) . '</pre>';
The easiest way to do this is with get_defined_vars().
From the Documentation
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.
Producing a var_dump of this array will provide you with an extensive list.
var_dump( get_defined_vars() );
A couple other interesting functions in the same area are get_defined_constants() and get_included_files()...