Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Scope

Status
Not open for further replies.

RaKKeR

Programmer
Nov 22, 2004
26
NL
Hi,

Is there a way to make all variables declared outside a function available for the function without having to write a long list like: global $var1, $var2, ... ?
I need this for an error handling function which has to prematurely end the php script after including a template page which uses those global vars. If not, is there any other way to make life easier in this case?

Thx in advance,

RaKKeR
 
Yes, its called $GLOBALS e.g.
Code:
<?php
$myvar = "test out the globals";
print_r ($GLOBALS);
$fred=$GLOBALS["myvar"];
echo "fred is " . $fred;
?>
I've created a variable ($myvar) and set it to a value. I then print out the entire $GLOBALS (which has just about everything in). And then set $fred to the value of myvar but taken directly from the $GLOBALS array using myvar as the key name.
Hope this helps
 
My suggestion to you is to turn your error function into a class. Then you only have to declare your variables in the class once (constructor). In your scripts, you can include the class file, create a new error class, then call it whenever you're trapping errors in your code.

Jon

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top