views:

571

answers:

6

sorry i'm a beginner and i can't determine how good a question this is, maybe it sounds utterly obvious to some of you.

if our use of these two below is the same which is better?

function doSomething ($var1,$var2,..){
    ...
}

OR

function doSomething (){
    global $var1,$var2,..;
    ...
}

by our use I mean that I know that in the second scenario we can also alter the global variables' value. but what if we don't need to do that, which is the better way of writing this function? does passing variables take less memory than announcing global's in a function?

+1  A: 

Write it to take parameters. Maintainability is far more important than micro-optimization. When you take parameters, the variables can not be modified in unexpected places.

Matthew Flaschen
+4  A: 

Avoid using global variables, use the passing variables in parameters approach instead. Depending on the size of your program, the performance may be negligible.

But if you are concerned with performance here are some key things to note about global variable performance with regards to local variables (variables defined within functions.)

  • Incrementing a global variable is 2 times slow than a local var.
  • Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.

Also, global variables increase the risk of using wrong values, if they were altered elsewhere inside your code.

Anthony Forloney
you link and information is more than useful, i wish i could also mark yours as the correct answer bc it is.
Mohammad
A: 

Pass in parameters, avoid globals. Keeping only the scope you need for a given situation is a measure of good code design. You may want to look at PHP variable scope...

http://php.net/manual/en/language.variables.scope.php

An excellent resource, with some pointers on what is best practices and memory management.

Urda
+4  A: 

The memory usage is a paltry concern. It's much more important that the code be easy to follow and not have... unpredicted... results. Adding global variables is a VERY BAD IDEA from this standpoint, IMO.

If you're concerned about memory usage, the thing to do is

function doSomething (&$var1, &$var2,..) {
   ...
}

This will pass the variables by reference and not create new copies of them in memory. If you modify them during the execution of the function, those modifications will be reflected when execution returns to the caller.

However, please note that it's very unusual for even this to be necessary for memory reasons. The usual reason to use by-reference is for the reason I listed above (modifying them for the caller). The way to go is almost always the simple

function doSomething ($var1, $var2) {
    ...
}
Arkaaito
that was a point i was missing, when our code starts to grow having the functions parameters there is helpful to know what the function will be using.
Mohammad
If $var1 and $var2 are read-only variables, they wouldn't consume more memory. Further reading, about PHP variables and references: http://derickrethans.nl/talks/phparch-php-variables-article.pdf
Dor
A: 

Hmm... I would really like it if someone would answer the question as asked - which is better for performance, the variable passed by reference or the global declaration. By convention, for example, Drupal uses a consistent and well-defined global variable $user; it would be nice to know if there would be any performance gain or loss by passing that variable to a new function instead of calling the global in that function.

David
Anthony Forloney answered that part of the question : )
Mohammad
A: 

Although it is not good practice as long as you guarantee that the global is never written, but only read you will have the flexibility of paramaters.

As as alternative, you can pass one parameter (or two if it really goes with the function, like exp) and the rest in an array of option (a bit like jquery does). This way you are not using globals, have some parameter flexibility and have clearly defined the defaults for each parameter.

function get_things($thing_name,$opt= array() {
   if(!isset($opt["order"])) $opt["order"]= 'ASC';
}
gssgss