31

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.

Hemi
  • 799
  • 1
  • 8
  • 12

5 Answers5

35

Yes, PHP $GLOBALS array will show you.

<?php

echo "<pre>";
print_r($GLOBALS);
echo "</pre>";
Jordan Arseno
  • 6,790
  • 8
  • 52
  • 96
  • It doesn't work for me. It throws `500 server error`. Meanwhile `var_dump($GLOBALS);` works. (hook: 'shutdown') Anyone, any idea? Thanks – Viktor Borítás Dec 19 '21 at 21:22
10

To get names of all global variables do this:

array_keys($GLOBALS)

More docs:

Tadeck
  • 125,377
  • 26
  • 148
  • 197
3

$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

Community
  • 1
  • 1
NullPoiиteя
  • 55,099
  • 22
  • 123
  • 139
2

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.

FThompson
  • 27,808
  • 11
  • 55
  • 91
2

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?

hjpotter92
  • 75,209
  • 33
  • 136
  • 171
  • If you **DO WANT** to have a [`var_export`](http://php.net/manual/en/function.var-export.php) then [check this](http://php.net/manual/en/function.var-export.php#100302) – hjpotter92 Sep 19 '12 at 07:28