PDA

View Full Version : function / sub ?


Roost3r
04-29-2003, 05:27 PM
I have some code i want to use in 2 areas of my program...

if(isset($_get['submit'])) {
someThing();
}

if(isset($_get['edit'])) {
someThing();
}

inside someThing() i get the vars from a form... Obviously this wont work because the scope of those vars is just inside the function... how could i get this to work so i can reuse the code; maybe declare the vars as Global in the function?

TIA

Spookster
04-29-2003, 05:54 PM
You should avoid putting variables into the global scope as it is a bad programming technique and can be dangerous in the long run if you are creating a large application.

You can return values from functions. Just return an array filled with the values from the form.

firepages
04-29-2003, 08:52 PM
note however if the values you are talking about are $_GET & $_POST that they are already global which may solve your problem as-is ?

Roost3r
04-29-2003, 08:59 PM
well im doing it liek this

$testVar = addslashes($_GET['stuff']);

so i dont believe the testvar is global

i might just put the GET in each of my mysql_query's in each of the if statements

firepages
04-29-2003, 09:36 PM
you are right in that $testVar is not global but
$_GET['stuff']=addslashes($_GET['stuff']);
should work just as well, it all depends what else you do with the variables as to whether this is the correct approach or not ?