Web lists-archives.com

Re: [PHP] Re: How Do I make Global Scope Variables Available toFunctions




On 29.05.2018 at 02:51, John wrote:

> Thanks to all of you who suggested this link.  Unfortunately it doesn't actually
> work.  If I set the variable as:
> 
> global $bad_line = "This line ......";
> 
> I get error:
> 
> [Mon May 28 20:28:48.491851 2018] [proxy_fcgi:error] [pid 7502:tid
> 139838662719232] [client 192.168.1.118:53916] AH01071: Got error 'PHP message:
> PHP Parse error:  syntax error, unexpected '=', expecting ',' or ';' in
> /httpd/iliffe/yrarc/test.php on line 3\n'

This is expected, since the global statement does not support initializers.

> If I code it this way:
> 
> global $bad_line;
> $bad_line = "This line ......";
> 
> then I get:
> 
> [Mon May 28 20:31:25.553000 2018] [proxy_fcgi:error] [pid 7502:tid
> 139838662719232] [client 192.168.1.118:53916] AH01071: Got error 'PHP message:
> PHP Notice: Undefined variable bad_line in /httpd/iliffe/yrarc/test.php on line
> 9\n'
> 
> Looking at the reference, this is "inside out"; that is, the variable is already
> defined in the global scope, that is it is in the scope of <?php ....?> and I
> want it to be available inside the function.  The reference is for variables
> defined within a function to make them available outside the function scope. 

The global statement is supposed to be used inside a function body, and
it creates a local variable which is actually a reference to the global
variable with the same name.  It does not matter if the global variable
is already set or not.

-- 
Christoph M. Becker

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php