Re: Re: How Do I make Global Scope Variables Available to Functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2018-05-28 at 09:37 +0200, Christoph M. Becker wrote:
> On 28.05.2018 at 02:46, John wrote:
> 
> > I am writing a PHP script that has a number of variables that (I think) are
> > in
> > Global scope; that is, they are defined inline before the first command of
> > the
> > script.  Note that these are not necessarily constants as shown in the
> > example I
> > included; most of them can be changed by the script.
> > 
> > I have functions defined following these variables and before the commands
> > in
> > the script appear, but when they are called, the functions report the
> > variables
> > as undefined.  This means I have to pass all the Globals to each function as
> > a
> > call argument.  
> > 
> > This seems odd but I don't see what I am doing wrong.  Any suggestions?
> 
> See
> <http://php.net/manual/en/language.variables.scope.php#language.variables.scop
> e.global>.
> 
> -- 
> Christoph M. Becker
> 
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'

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. 

I ***could*** pass these variables as arguments as shown in the original
example, but in the case at hand there are dozens of them and I have been
passing them all in an array.  This leads to several problems:

1.  I have to set up an array every place I call a function 

2.  I have to keep track of the array indexes for each variable

3.  I'm sure that the server will eventually choke on the increased work of
handling dozens of arrays; I haven't checked this as I'm still writing the
scripts

4.  I'm sure there is excessive memory usage passing everything by reference.

So, any ideas?

And,again, thanks for this suggestion.

John




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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux