CodeChargenewbie
Posts: 114
|
| Posted: 09/28/2007, 1:18 PM |
|
what is the difference between var and global?
Thank you very much.
|
 |
 |
wkempees
|
| Posted: 09/28/2007, 3:27 PM |
|
var, defined in a function is LOCAL to that function.
global, makes a previously defined var (in another 'higher' function)
available.
function somefunction (
var $MyVar; // local to this function
global $YourVar; // YourVar defined somewhere else
$MyVar = 1;
$YourVar = 2;
Return;
)
Suppose:
$MyVar=10;
$YourVar =10;
somefunction(); we call the function
echo $MyVar; // will return 10, unchanged.
echo $YourVar; // will return 2, changed by the function
Hope this explains that GLOBAL means global!
Walter
|
|
|
 |
CodeChargenewbie
Posts: 114
|
| Posted: 09/28/2007, 3:59 PM |
|
Quote wkempees:
var, defined in a function is LOCAL to that function.
global, makes a previously defined var (in another 'higher' function)
available.
function somefunction (
var $MyVar; // local to this function
global $YourVar; // YourVar defined somewhere else
$MyVar = 1;
$YourVar = 2;
Return;
)
Suppose:
$MyVar=10;
$YourVar =10;
somefunction(); we call the function
echo $MyVar; // will return 10, unchanged.
echo $YourVar; // will return 2, changed by the function
Hope this explains that GLOBAL means global!
Walter
I think it does, thank you! I thought var was only used in defining classes; you wouldn't define variables in a class using global.
So, global in your example is almost like a public class variable or a referenced parameter of a function like this, where what you do within the function may affect the value of the variable elsewhere.
I think I get confused because I will define variables outside of function blocks using global. So, within the scope of the "main" code, these are all equivalent then:
$var = 0;
var $var1;
global $var2;
???
That is, all of the above are written to the super global array and thus, again, within the scope of the "main" code, global and var mean the same thing, right?
|
 |
 |
mamboBROWN
Posts: 1713
|
| Posted: 09/30/2007, 9:13 AM |
|
CodeChargenewbie
For future references you might want to download the PHP chm ( http://us2.php.net/download-docs.php ). It can be very helpful in answering questions like this.
|
 |
 |
CodeChargenewbie
Posts: 114
|
| Posted: 10/01/2007, 6:24 AM |
|
Thanks. I examined both php.net and phpfreaks and couldn't find the information I seeked; so, I poised the question on this forum. I'm still a bit perplexed about the functionality of var and global in the "main" scope of the code, however.
I know var is local and global is global. But I'm curious as to how the $_GLOBALS array treats both within the "main" scope. If I have this:
<?php
$var = 5;
var $another_var;
global $yet_another_var;
?>
Are there any differences between var and global, as to how php will parse this code? Are all three of these variables essentially treated equally in this particular instance?
I noticed something interesting, as it to pertains to $_GLOBALS, perhaps.
Take a look at this code:
<?php
var $test;
global $test2;
global $test3;
$test = 5;
echo $test; // prints out 5
example();
echo $test; // prints out 6
//
//
function example()
{
global $test;
global $test2;
global $test3;
$test = 6;
}
?>
After I type "global $" inside of function example(), a drop-down window springs up with the variables $test2 and $test3 but $test does not show up. Yet, I can still type "global $test;" and manipulate the variable as I wish. So, it seems the $_GLOBALS array stores variables defined as vars and globals, but it only shows the ones defined as globals in drop down boxes. I'm curious as to why.
|
 |
 |
|