Re: Re: How Do I make Global Scope Variables Available to Functions
| From: | John | Date: | Tue, 29 May 2018 00:51:44 +0000 |
| Subject: | Re: Re: How Do I make Global Scope Variables Available to Functions | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326490@lists.php.net to get a copy of this message | ||
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