[thelist] Global variables in functions?

David Bindel dbindel at austin.rr.com
Tue Aug 19 23:56:41 CDT 2003


Joel wrote:
> When I do this:
> 
> --
>   $url = 'http://server/script.php?'.$_SERVER[QUERY_STRING];
>   $output = file_get_contents ($url);
> 
> function body() {
>   echo $output;
> }
> --
> 
> body() ends up empty.
> What am I missing here?

Global variables are stored in the $GLOBALS superglobal array, so you
have to use $GLOBALS['output'] to get to the $output global when you
aren't in global scope.

> But when I do this:
> 
> --
> function body() {
> 
>   $url = 'http://server/script.php?'.$_SERVER[QUERY_STRING];
>   $output = file_get_contents ($url);
>   echo $output;
> 
> }
> --
> 
> it works fine.

That's because $output is in the scope of that function.  In the
previous code chunk you gave, $output was outside of the scope of the
function (except through the $GLOBALS array.)

HTH,
David

--
    David I. Bindel
  Website Development
 dbindel at austin.rr.com
  www.davidbindel.com



More information about the thelist mailing list