lock
lock

Reputation: 6614

in php is there a way to dump "all" variable names with their corresponding value?

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?

Upvotes: 15

Views: 35114

Answers (3)

rojoca
rojoca

Reputation: 11190

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>';

Upvotes: 44

grantwparks
grantwparks

Reputation: 1153

A couple other interesting functions in the same area are get_defined_constants() and get_included_files()...

Upvotes: 2

Josiah
Josiah

Reputation: 3332

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() );

Upvotes: 14

Related Questions