Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 328
Mohammad Avatar asked Feb 07 '10 07:02

Mohammad


People also ask

What is the disadvantage of global variables?

Disadvantages of using Global VariablesToo many variables declared as global, then they remain in the memory till program execution is completed. This can cause of Out of Memory issue. Data can be modified by any function. Any statement written in the program can change the value of the global variable.

What is the benefit to passing parameters in and out of function instead of using global variables?

Parameter passing is usually more memory efficient because the memory allocated to the parameters is automatically de-allocated when the function returns to the main program. It is then available for the next function call to use.

Why are parameters better than global variables?

Parameter passing - allows the values of local variables within the main program to be passed to sub-programs without the need to use global variables. The value of these variables (or a copy of the value of these variables) is passed as a parameter to and from sub-programs as necessary.

What are the benefits of using parameters in functions?

by using function parameters, you start on your journey into clean pure functions, where the function only returns an answer based in its inputs. You can then progress onto not having your functions touch anything outside of itself, so debugging is easy. You pass in parameters and check the output.


1 Answers

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) {     ... } 
like image 129
Arkaaito Avatar answered Nov 12 '22 11:11

Arkaaito