Page 1 of 1

Registering variables

Posted: Sun Oct 27, 2002 6:28 am
by f1nutter
Is there anything in PHP that I can use that registers / lists all the variables used in a script.

I would like to know what variables I have used, so that I know correct assignments have been made, and a value has not been passed to a variable which will not be used again because I made a typo mistake, and the value is lost, and when the correct variable is used it has the wrong value.

For example, I might have made a typo in a variable name within an if clause, and the script works fine except bizzare circumstances when the if clause is used and something goes wrong, like validating all credit card numbers no matter what was inputted.

Code: Select all

<?php
$variable = 10;

if ($variable > 5)
{
 $Variable = $variable / 2;
}
else
{
 $variable = $variable * 2;
}

print($variable);

?>
In this case the wrong value is given because a new variable was created. (Assuming PHP is case-sensitive, I think is is!).

Any ideas?

Posted: Sun Oct 27, 2002 7:23 am
by volka

Posted: Sun Oct 27, 2002 12:26 pm
by f1nutter
Thats very nice, a lot of interesting and useful information, but it doesn't list the actual variables used in my script.

For the above example, it would like it to return $variable and $Variable.

Any more suggestions? Maybe I could write a script to extract the variables from a given file name. Something that does a regular expression match and fill an array. Would anyone else find that useful?

Posted: Sun Oct 27, 2002 12:34 pm
by hob_goblin
f1nutter wrote:Thats very nice, a lot of interesting and useful information, but it doesn't list the actual variables used in my script.
try putting:

Code: Select all

$arr = get_defined_vars();
echo "<pre>"; print_r($arr) echo "</pre>";
at the bottom.

Posted: Sun Oct 27, 2002 4:05 pm
by f1nutter
Yep, done that and it lists SERVER variables, argc and argv etc. but not the actual variable names.

My test page has a function in it, could that be the problem?

Code: Select all

&lt;?php
main();

function main()
{
 // do something
}

?&gt;
C background!

Posted: Sun Oct 27, 2002 4:34 pm
by hob_goblin
it prints them out, at the very bottom.

Posted: Sun Oct 27, 2002 5:45 pm
by volka

Code: Select all

&lt;?php function someFunc()
{
	$funcVar = 5;
}

$variable = 10; 
if ($variable &gt; 5) 
 $Variable = $variable / 2; 
else 
 $variable = $variable * 2; 

print($variable); 

$arr = get_defined_vars(); 
echo "&lt;pre&gt;"; print_r($arr); echo "&lt;/pre&gt;";?&gt;
:arrow:
...
[_REQUEST] => Array
(
)

[variable] => 10
[Variable] => 5
but of course $funcVar isn't in there; out-of-scope ;)

Posted: Mon Oct 28, 2002 2:52 am
by f1nutter
f1nutter wrote:

Code: Select all

&lt;?php
main();

function main()
{
 // do something
}

?&gt;
Thanks volka, you make a good point - all my variables are out of scope at the end of the script. The main part of the script doesn't actually have any variables, just a function call! Maybe if I use get_defined_vars(); at the end of each function, and just add to the array, that would work, wouldn't it?

Posted: Mon Oct 28, 2002 4:54 am
by volka
probably,
can't think of a way to automate this (but maybe there is one)