21

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?

Mohammad
  • 7,344
  • 15
  • 48
  • 76
  • 2
    In addition to the reasons for not using "global" given in the answers below, consider also code reusability. If, for instance, you have a utility file of functions, then in any script that includes that file you have to make sure that your variable naming matches the global variables in those functions. This can get problematic, especially with a complex script. Or if you want to refactor a script by "plugging in" an already-existing function, then you'll need to check all the script's code, even code not pertaining to the function, to make sure there are no naming conflicts. – GZipp Feb 07 '10 at 08:59
  • I'd like to add that you might also consider adding the global variable declaration outside your function, but this is still bad practice, because someone else might not know if they are importing your code. They might not be aware they are importing that global variable and create another variable with the same name, thus overriding your original. – imagineerThat May 31 '13 at 23:51

6 Answers6

24

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
  • 7,347
  • 3
  • 39
  • 56
  • 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 Feb 07 '10 at 08:23
  • 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 Feb 07 '10 at 08:32
  • It is not correct, going by reference is generally more inefficient. It is very wrong to say that when passing a parameter, a copy of the value is created. A copy is only created when its value is modified. – Joseph Feb 07 '20 at 18:37
16

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
  • 90,123
  • 14
  • 117
  • 115
  • you link and information is more than useful, i wish i could also mark yours as the correct answer bc it is. – Mohammad Feb 07 '10 at 08:24
6

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
  • 278,309
  • 50
  • 514
  • 539
2

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
  • 21
  • 1
1

As of PHP 4 using global with big variables affects performance significantly.

Having in $data a 3Mb string with binary map data and running 10k tests if the bit is 0 or 1 for different global usage gives the following time results:

function getBit($pos) {
global $data;
$posByte = floor($pos/8); 
...
}

t5 bit open: 0.05495s, seek: 5.04544s, all: 5.10039s

function getBit($data) {
 global $_bin_point;
 $pos = $_bin_point; 
 $posByte = floor($pos/8); 
}

t5 bit open: 0.03947s, seek: 0.12345s, all: 0.16292s

function getBit($data, $pos) {
$posByte = floor($pos/8); 
...
}

t5 bit open: 0.05179s, seek: 0.08856s, all: 0.14035s

So, passing parameters is way faster than using global on variables >= 3Mb. Haven't tested with passing a $&data reference and haven't tested with PHP5.

Ilya Sheershoff
  • 409
  • 4
  • 10
1

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
  • 5,460
  • 5
  • 34
  • 47