faqts : Computers : Programming : Languages : PHP : kms

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

22 of 33 people (67%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

For maximum performance, should I use global vars when calling my own function, or should I pass the required vars as args?

May 17th, 2000 18:19
Marek Narkiewicz, Steven Campbell, www.php.net/manual


As I see it the most efficient and thus faster way would be to declare 
the variables global within the function. The tidiest code would result 
from passing the variables to the function. Therefore a good compromise 
would be to pass the variables to the function by reference rather than 
by value.
function foo($arg){
    echo $arg;
}
$word = "sheep";
foo(&$word);

this will echo "sheep"
This is more efficient as the variable in the function is only a 
pointer to the variable outside the function and can help you to 
maintain good code.
hth.