note 79957 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:30:44 +0000 |
| Subject: | note 79957 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155549@lists.php.net to get a copy of this message | ||
Note Submitter: pepesantillan at gmail dot com
----
allan on 12-Sep-2006 10:53 wrote:
Using the global keyword inside a function to define a variable is essentially the same as passing
the variable by reference as a parameter:
somefunction(){
global $var;
}
is the same as:
somefunction(& $a) {
}
The advantage to using the keyword is if you have a long list of variables needed by the function -
you dont have to pass them every time you call the function.
---------------------------------------------------------------
Just wanted to point out that using global and using a reference is NOT the same. Why?
Imagine you just called the function somefunction() defined by out friend here from another
function, say otherfunction().
If you use the global example (the 1st one), you will create a global variable $var for your whole
php script.
But if you use the 2nd example where you reference a variable, you could be ONLY changing the value
of some variable in the function that called the function somefunction() - in this case
otherfunction() - and not creating a new global variable for your whole php script.
I hope some1 reading and understanding here creates an example about this. Im so lazy at doing that.
Hope you got the idea, and have as much fun learning to use php as I am! (yay! variables variables
tutorial is next! xD)