Get Defined Variables in Current Scope
Jun 14th, 2008
<?php
session_start();
setcookie('name', 'satya');
$testVar = "variable value";
$_SESSION['sess_var'] = 'sess_var';
echo '<pre>';
$msg = 'hello';
// not to mention in list of variables defined
$nothingToThinkAbout = 'do not consider in current scope';
function testScopeVarInFuncton()
{
global $testVar;
$localVar = 'a var within function';
include 'curr_vars.inc.php';
}
//testScopeVarInFuncton();
include 'curr_vars.inc.php';
?>
<form method=post action="">
<input type=text value="" name="age">
<input type=submit name="s">
</form>
The file which can be included in any php page and then that will tell all the variables defined in current scope.
Page:: curr_vars.inc.php
Page:: curr_vars.inc.php
<?php
// Get all defined vars upto here.
$definedVars = array_keys(get_defined_vars());
// list of variables not to be considered for scope
// if variable $nothingToThinkAbout is defined then do not consider
$doNotConsider = str_replace(' ', '', 'CONSOLE,GLOBALS,SELINUX_INIT,TERM,INIT_VERSION,PATH,nothingToThinkAbout,RUNLEVEL,PWD,LANG,PREVLEVEL,SHLVL,HOME,_,HTTP_HOST,HTTP_USER_AGENT,HTTP_ACCEPT,HTTP_ACCEPT_LANGUAGE,HTTP_ACCEPT_ENCODING,HTTP_ACCEPT_CHARSET,HTTP_KEEP_ALIVE,HTTP_CONNECTION,HTTP_CACHE_CONTROL,SERVER_SIGNATURE,SERVER_SOFTWARE,SERVER_NAME,SERVER_ADDR,SERVER_PORT,REMOTE_ADDR,DOCUMENT_ROOT,SERVER_ADMIN,SCRIPT_FILENAME,REMOTE_PORT,GATEWAY_INTERFACE,SERVER_PROTOCOL,REQUEST_METHOD,QUERY_STRING,REQUEST_URI,SCRIPT_NAME,PHP_SELF,PATH_TRANSLATED,argv,argc,HTTP_POST_VARS,HTTP_GET_VARS,HTTP_COOKIE_VARS,HTTP_SERVER_VARS,_SERVER,HTTP_ENV_VARS,_ENV,HTTP_POST_FILES,HTTP_POST_FILES,_FILES,_REQUEST,HTTP_SERVER_VARS,HTTP_COOKIE,HTTP_SESSION_VARS,PHPSESSID');
$arr1 = explode (',', $doNotConsider);
$vars = array_diff($definedVars, $arr1);
foreach ($vars as $k=>$v) {
if (! is_array($$v)) {
echo "111 . $v" . '=>' . $$v;
}
else if (count($v) > 0){
echo "222 . $v" . '=>';
print_r($$v);
}
echo '<br>';
}
?>
Possibly Related posts:


Test page for checking defined variables in current scope: