Noir
Noir

Reputation: 484

Get a list of names of all global variables in PHP

I'm working with Moodle and theres a lot of global variables and it seems that there's no proper documentation. First I thought like "yeah just see what echo "<pre>",print_r($GLOBALS,1),"</pre>"; will yield". But when I try it, my browser becomes unresponsive while rendering and the rendered result is a mess. Unfortunately I don't have the possibility to use an debugger since the xdebug package is not available for the distro I'm using. (Turnkey Moodle appliance)

So my question is: Is there a way to see just the names of all global variables so that I can inspect them individually?

Upvotes: 1

Views: 1701

Answers (4)

davosmith
davosmith

Reputation: 6327

Easiest answer - open up lib/setup.php, scroll down to approx line 418 and they are all defined there.

Whilst you're at it, Moosh can also generate a file that allows autocompletion of all the $CFG-> variables in your Moodle site (not directly answering the question, but closely related).

Upvotes: 2

Roman Angelovskij
Roman Angelovskij

Reputation: 71

maybe array_keys($GLOBALS)&

Upvotes: 2

rray
rray

Reputation: 2556

Use get_defined_vars() to get all variables in that scope, that could be in all script or in a specific function.

echo '<pre>';
print_r(get_defined_vars());

Upvotes: 1

Ian
Ian

Reputation: 25336

As you pointed out, $GLOBALS is in fact the way to see all global variables.

The reason your script is dying is because you have a large number of variables, and each of them likely have many child objects (possibly even recursively).

If you just want a list of the names of the variables, just do var_dump(array_keys($GLOBALS)).

Upvotes: 1

Related Questions