Forum Moderators: coopster

Message Too Old, No Replies

How can I get all variable names that have been used?

         

knnknn

11:36 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



I want to get a list of all variable names that have been used in the script:

<?php
$foo = 'blue';
$bar = 'red;

echo listofallvariablename();
?>

Should create 'foo', 'bar'

Is there a function like that?

knnknn

11:51 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



OK, case closed.

I can fetch all names via $GLOBALS or get_defined_vars()

coopster

3:14 am on Aug 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Bingo. Just be careful when you look at your data if you are running the function from inside a user-defined function. Variable scope may come into play here! Example:
<pre> 
<?php
function checkVariables()
{
$a = 1;
print "Within the function:\n";
print_r(get_defined_vars());
}
$b = 2;
checkVariables();
print "Outside the function:\n";
print_r(get_defined_vars());
?>
</pre>