Registering variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Registering variables

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post 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?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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.
Last edited by hob_goblin on Sun Oct 27, 2002 6:28 pm, edited 1 time in total.
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post 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!
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

it prints them out, at the very bottom.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably,
can't think of a way to automate this (but maybe there is one)
Post Reply