Re: Globals funkiness
| From: | Steve Edberg | Date: | Tue, 16 Oct 2001 20:47:06 +0000 |
| Subject: | Re: Globals funkiness | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71258@lists.php.net to get a copy of this message | ||
At 3:19 PM -0500 10/16/01, Boget, Chris wrote:
Consider (and run) the following code: function globalize() { global $STATEMENT_DATE_MACRO; global $STATEMENT_MONTH_MACRO; global $SETTLEMENT_DATE_MACRO; echo "$STATEMENT_DATE_MACRO = STATEMENT_DATE_MACRO<br>\n"; echo "$STATEMENT_MONTH_MACRO = STATEMENT_MONTH_MACRO<br>\n"; echo "$SETTLEMENT_DATE_MACRO = SETTLEMENT_DATE_MACRO<br>\n"; } //function doIt() {the global statement doesn't work this way; you have to use it WITHIN each function you want to access globals in. It will work if you stick the three global statements after the function declaration. There has been a lot of discussion in the past about PHP's use of globals; you can search the mailing list at http://marc.theaimsgroup.com for more on that debate. Also see http://www.php.net/manual/en/language.variables.scope.php -steve
-- +--------------- my people are the people of the dessert, ---------------+ | Steve Edberg sbedberg@ucdavis.edu | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | +---------------- said t e lawrence, picking up his fork ----------------+$today = date( "U" );$last_month = mktime( 0, 0 , 0 , ( date( "m" ) - 1 ), 1 , date( "Y" )); $STATEMENT_DATE_MACRO = date( "jS F, Y", $today ); $STATEMENT_MONTH_MACRO = date( "F Y", $last_month ); $SETTLEMENT_DATE_MACRO = date( "F 20, Y", $today ); globalize(); //} //doIt(); Everything works great. Couldn't be happier. Now, uncomment the three lines above that are commented out... the lines that start and end the "doIt()" function and the function call itself. Run it again. Why isn't this working? Shouldn't the calling function's variables be part of the "global namespace" for the called functions? Chris