Page 1 of 1

repeat variable in functions.php

Posted: Wed Aug 11, 2010 6:53 pm
by codeline
I've got a functions.php file that holds all my basic functions of displaying data from queries, etc.

I was curious, can I repeatedly set the same variable within different functions without error?

For example..

Code: Select all

function myFunctionHere(){

$q = "SELECT * FROM etc";
$result = mysql_query($q);

}

function aSeperateFunctionHere(){

$q = "SELECT * FROM etc";
$result = mysql_query($q);

}

Is there any conflict if I were to use 2 functions in the same page, etc.?

Re: repeat variable in functions.php

Posted: Wed Aug 11, 2010 7:07 pm
by josh
This is the whole point of functions. Each has their own scope. So no, the variables will not interfere if they are not "globals" (in your example they are not). This is why people hate long spaghetti code. The $i variable from one loop may conflict with the $i variable from some other operation as the programmer moves around code. But if the variable usage is encapsulated in a function, each function can be moved around without fear of un-intended consequence.