Re: Globals funkiness
| From: | Alex Shi | Date: | Tue, 16 Oct 2001 21:51:14 +0000 |
| Subject: | Re: Globals funkiness | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71269@lists.php.net to get a copy of this message | ||
> 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";
>
> }
>
> file://function doIt() {
> $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();
>
> file://}
>
> file://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
>
In PHP keywords "global" only works to cause a global variable to
be seen within a specific namespace/function. On this aspect PHP
is slightly different from C. In C namespace is hierachical and thus
a vairable can be seen in downward without "global" re-declaration
within a certain function.
Alex