The advantage / disadvantage between global variables and function parameters in PHP?

33,894

Solution 1

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) {
    ...
}

Solution 2

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.

Solution 3

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.

Solution 4

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';
}

Solution 5

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.

Share:
33,894
Mohammad
Author by

Mohammad

Beginner programmer, trying to learn new things : )

Updated on February 11, 2020

Comments

  • Mohammad
    Mohammad about 4 years

    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
    Mohammad about 14 years
    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
    Mohammad about 14 years
    you link and information is more than useful, i wish i could also mark yours as the correct answer bc it is.
  • Dor
    Dor about 14 years
    If $var1 and $var2 are read-only variables, they wouldn't consume more memory. Further reading, about PHP variables and references: derickrethans.nl/talks/phparch-php-variables-article.pdf
  • Joseph
    Joseph about 4 years
    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.