View Single Post
Old 06-08-2008, 04:42 PM   #6 (permalink)
delayedinsanity
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

If I get you right, you're asking why, if you've already declared a variable as global once in a script, would you have to redeclare it as global anywhere else, as in, once you've stated it's a global, how come the entire script just doesn't immediately recognize it as a global? I *think* this may be explained by how the global keyword is working.

Technically all variables defined outside of a function are considered 'global'. They can be used anywhere within the script, including from within a function so long as you access them via the $GLOBALS array or with the global keyword. This is because functions are almost like a private namespace, where anything you do inside of them is seperate from the global scope of the script. So when you do;

PHP Code:
function globalVars() {
    global 
$szString;
    echo 
$szString;

    
//or

    
echo $GLOBALS['szString'];

...you're not actually renaming or redeclaring $szString as a global, you're just telling the function itself to use the global version of $szString as opposed to it's own local copy. Thus why you have to do it in every UDF you're using it in, because you're telling those specific functions to use the global copy instead of their own copy.

Was that even remotely closer to what you were looking for?
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Dave (06-10-2008)