How to retrieve variables defined in a particular include file in php? -
i want know variables have been declared within include file.
example code is:
include file:
<?php $a = 1; $b = 2; $c = 3; $myarr = array(5,6,7); ?>
main file:
<?php $first = get_defined_vars(); include_once("include_vars.php"); $second = get_defined_vars(); $diff = array_diff($second ,$first); var_dump($diff); $x = 12; $y = 13; ?>
my attempt use difference of get_defined_vars() not seem give accurate information. gives:
array(2) { ["b"]=> int(2) ["c"]=> int(3) }
$a , $myarr seem missing. other methods can use?
it's not thing this, hope it's needed debugging. should find difference between keys of arrays get_defined_vars
return, not between values:
$diff = array_diff(array_keys($second),array_keys($first));
if don't want include file, more complicated way using tokenizer
:
$tokens = token_get_all(file_get_contents("include_vars.php")); foreach($tokens $token) { if ($token[0] == t_variable) echo $token[1] . ', '; }
that return variables, non-globals ones , ones unset in script (!)
Comments
Post a Comment