NYCPHP Meetup

NYPHP.org

[nycphp-talk] global variables

Bhulipongsanon, Pinyo Pinyo.Bhulipongsanon at usa.xerox.com
Thu May 22 12:22:09 EDT 2003


Hi Chris,

Thanks.  Oops, I figured it out as I was writing this.

Essentially, my web page call a template.php file.  The body text is already
inside a function called render_body() that's why variable that should be
global is not global (because it is in local scope of a function).  Well, in
this case, I can still force local variable into global scope by doing
something like this:

function render_body() {
	$var1 = "this is a locally defined variable";
	$_GET['var1'] = $var1;  // $var1 is local variable defined in
render_body() function
}

Then in another function call, the variable like this

function someOtherFunction() {
	$var1 = $_GET['var1']; // because using global $var1 will not work
	echo $var1;
}

Is this okay?

Thank you,
Pinyo


-----Original Message-----
From: Chris Snyder [mailto:chris at psydeshow.org]
Sent: Thursday, May 22, 2003 11:22 AM
To: NYPHP Talk
Subject: Re: [nycphp-talk] global variables


Bhulipongsanon, Pinyo wrote:

>Hi all,
>
>I have questions regarding global.  It appears that my program that uses
>global no longer work consistently.  I think because global has been turned
>off in newer server.  I only need global variables so that functions can
use
>them, however I really do not want to pass everything by value to the
>function.
>
>So this code is out:
>
>global $var1, $var2;
>
>  
>
That doesn't seem entirely right-- global $var1, $var2; should still 
work just fine. What's been turned off is probably "register_globals" 
which only applies to server environment and CGI request variables, not 
variables that you declare at runtime.

file:///usr/local/share/doc/php/configuration.directives.html#ini.register-g
lobals

If either $var1 or $var2 are really $_GET['var1'] or $_GET['var2], then 
you'll need to explicitly assign them in global scope like so:

<?php

function foo() {
    global $var1, $var2;
    return $var2.$var1;
    }

$var1= $_GET['var1'];
$var2= $_SERVER['DOCUMENT_ROOT'];

?>

Should work...








--- Unsubscribe at http://nyphp.org/list/ ---




More information about the talk mailing list