repeat variable in functions.php

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
codeline
Forum Newbie
Posts: 12
Joined: Tue Aug 10, 2010 5:53 am

repeat variable in functions.php

Post 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.?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: repeat variable in functions.php

Post 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.
Post Reply