note 79957 added to language.variables.scope
| From: | pepesantillan at gmail dot com | Date: | Fri, 21 Dec 2007 02:36:31 +0000 |
| Subject: | note 79957 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133830@lists.php.net to get a copy of this message | ||
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)
----
Server IP: 69.147.83.197
Probable Submitter: 190.42.202.68
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/79957
Del: integrated -- https://master.php.net/note/delete/79957/integrated
Del: useless -- https://master.php.net/note/delete/79957/useless
Del: bad code -- https://master.php.net/note/delete/79957/bad+code
Del: spam -- https://master.php.net/note/delete/79957/spam
Del: non-english -- https://master.php.net/note/delete/79957/non-english
Del: in docs -- https://master.php.net/note/delete/79957/in+docs
Del: other reasons-- https://master.php.net/note/delete/79957
Reject -- https://master.php.net/note/reject/79957
Search -- https://master.php.net/manage/user-notes.php