Welcome! Log In Create A New Profile

Advanced

[PHP] How Do I make Global Scope Variables Available to Functions

Posted by John 
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.

A trivial script illustrating the problem is below.

This seems odd but I don't see what I am doing wrong. Any suggestions?

PHP 5.6.30 called through php-fpm on Apache.

Thanks in advance.

John
------------------------------------
test.php - working version

This variant passes a variable in global scope explicitly as a parameter. It
displays correctly on the browser screen.


<?php

$bad_line = "This line should display on screen.";

// sample function that works
function sample1($bad_line)
{
return "\n" . $bad_line . "\n";
}

// this call does work
echo sample1($bad_line);

exit;
?>



test.php - doesn't work

This variant does not work; it throws error:

[Sun May 27 20:17:46.779348 2018] [proxy_fcgi:error] [pid 893:tid
139838497978112] [client 192.168.1.104:37732] AH01071: Got error 'PHP message:
PHP Notice: Undefined variable: bad_line in /httpd/iliffe/yrarc/test.php on
line 8\n'

<?php

$bad_line = "This line should display on screen.";

// sample function that doesn't work
function sample1()
{
return "\n" . $bad_line . "\n";
}

// this call doesn't work
echo sample1();

exit;

?>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On May 27, 2018, at 5:46 PM, 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.
>
> A trivial script illustrating the problem is below.
>
> This seems odd but I don't see what I am doing wrong. Any
> suggestions?
>
> PHP 5.6.30 called through php-fpm on Apache.
>
> Thanks in advance.
>
> John
> ------------------------------------
> test.php - working version
>
> This variant passes a variable in global scope explicitly as a
> parameter. It
> displays correctly on the browser screen.
>
>
> <?php
>
> $bad_line = "This line should display on screen.";
>
> // sample function that works
> function sample1($bad_line)
> {
> return "\n" . $bad_line . "\n";
> }
>
This function could also be written
function sample1($a)
{
return "\n" . $a . "\n";
}
> echo sample1($bad_line);

But it looks like you've answered your own question.
If you wanted to have the function, or another function
alter $bad_line, it would need to be passed by reference

echo sample1(&$bad_line) .... as I understand it.

> // this call does work
> echo sample1($bad_line);
>
> exit;
> ?>
>
>
>
> test.php - doesn't work
>
> This variant does not work; it throws error:
>
> [Sun May 27 20:17:46.779348 2018] [proxy_fcgi:error] [pid 893:tid
> 139838497978112] [client 192.168.1.104:37732] AH01071: Got error
> 'PHP message:
> PHP Notice: Undefined variable: bad_line in /httpd/iliffe/yrarc/
> test.php on
> line 8\n'
>
> <?php
>
> $bad_line = "This line should display on screen.";
>
> // sample function that doesn't work
> function sample1()
> {
> return "\n" . $bad_line . "\n";
> }
>
> // this call doesn't work
> echo sample1();
>
> exit;
>
> ?>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
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.scope.global>;.

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
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
On Sun, 2018-05-27 at 19:23 -0700, Jeffry Killen wrote:
> On May 27, 2018, at 5:46 PM, 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.
> >
> > A trivial script illustrating the problem is below.
> >
> > This seems odd but I don't see what I am doing wrong. Any
> > suggestions?
> >
> > PHP 5.6.30 called through php-fpm on Apache.
> >
> > Thanks in advance.
> >
> > John
> > ------------------------------------
> > test.php - working version
> >
> > This variant passes a variable in global scope explicitly as a
> > parameter. It
> > displays correctly on the browser screen.
> >
> >
> > <?php
> >
> > $bad_line = "This line should display on screen.";
> >
> > // sample function that works
> > function sample1($bad_line)
> > {
> > return "\n" . $bad_line . "\n";
> > }
> >
>
> This function could also be written
> function sample1($a)
> {
> return "\n" . $a . "\n";
> }
> > echo sample1($bad_line);
>
> But it looks like you've answered your own question.
> If you wanted to have the function, or another function
> alter $bad_line, it would need to be passed by reference
>
> echo sample1(&$bad_line) .... as I understand it.
>
> > // this call does work
> > echo sample1($bad_line);
> >
> > exit;
> > ?>
> >
> >
> >
> > test.php - doesn't work
> >
> > This variant does not work; it throws error:
> >
> > [Sun May 27 20:17:46.779348 2018] [proxy_fcgi:error] [pid 893:tid
> > 139838497978112] [client 192.168.1.104:37732] AH01071: Got error
> > 'PHP message:
> > PHP Notice: Undefined variable: bad_line in /httpd/iliffe/yrarc/
> > test.php on
> > line 8\n'
> >
> > <?php
> >
> > $bad_line = "This line should display on screen.";
> >
> > // sample function that doesn't work
> > function sample1()
> > {
> > return "\n" . $bad_line . "\n";
> > }
> >
> > // this call doesn't work
> > echo sample1();
> >
> > exit;
> >
> > ?>
> >
> >
Thanks Jeffry. Yes, I do have a workaround as you suggested but given the
number of variables being passed it is unworkable to have to pass them all as
arguments to the function. I have been passing them as an array as I noted in
an earlier reply but that is getting unwieldy and will be a maintenance
nightmare.

The original variables, so far anyway, are not changed within any function but
can vary between calls to the various functions. Basically it is running in a
loop that sets up the various required variables and then passes everything to
one or another function for database processing and/or printing.

Thanks for the suggestion though. I'm getting an education here!

John

>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
> On May 28, 2018, at 5:58 PM, John <[email protected]> wrote:
>
> On Sun, 2018-05-27 at 19:23 -0700, Jeffry Killen wrote:
>> On May 27, 2018, at 5:46 PM, 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.
>>>
>>> A trivial script illustrating the problem is below.
>>>
>>> This seems odd but I don't see what I am doing wrong. Any
>>> suggestions?
>>>
>>> PHP 5.6.30 called through php-fpm on Apache.
>>>
>>> Thanks in advance.
>>>
>>> John
>>> ------------------------------------
>>> test.php - working version
>>>
>>> This variant passes a variable in global scope explicitly as a
>>> parameter. It
>>> displays correctly on the browser screen.
>>>
>>>
>>> <?php
>>>
>>> $bad_line = "This line should display on screen.";
>>>
>>> // sample function that works
>>> function sample1($bad_line)
>>> {
>>> return "\n" . $bad_line . "\n";
>>> }
>>>
>>
>> This function could also be written
>> function sample1($a)
>> {
>> return "\n" . $a . "\n";
>> }
>>> echo sample1($bad_line);
>>
>> But it looks like you've answered your own question.
>> If you wanted to have the function, or another function
>> alter $bad_line, it would need to be passed by reference
>>
>> echo sample1(&$bad_line) .... as I understand it.
>>
>>> // this call does work
>>> echo sample1($bad_line);
>>>
>>> exit;
>>> ?>
>>>
>>>
>>>
>>> test.php - doesn't work
>>>
>>> This variant does not work; it throws error:
>>>
>>> [Sun May 27 20:17:46.779348 2018] [proxy_fcgi:error] [pid 893:tid
>>> 139838497978112] [client 192.168.1.104:37732] AH01071: Got error
>>> 'PHP message:
>>> PHP Notice: Undefined variable: bad_line in /httpd/iliffe/yrarc/
>>> test.php on
>>> line 8\n'
>>>
>>> <?php
>>>
>>> $bad_line = "This line should display on screen.";
>>>
>>> // sample function that doesn't work
>>> function sample1()
>>> {
>>> return "\n" . $bad_line . "\n";
>>> }
>>>
>>> // this call doesn't work
>>> echo sample1();
>>>
>>> exit;
>>>
>>> ?>
>>>
>>>
> Thanks Jeffry. Yes, I do have a workaround as you suggested but given the
> number of variables being passed it is unworkable to have to pass them all as
> arguments to the function. I have been passing them as an array as I noted in
> an earlier reply but that is getting unwieldy and will be a maintenance
> nightmare.
>
> The original variables, so far anyway, are not changed within any function but
> can vary between calls to the various functions. Basically it is running in a
> loop that sets up the various required variables and then passes everything to
> one or another function for database processing and/or printing.
>
> Thanks for the suggestion though. I'm getting an education here!
>
> John
>
>>

When I have multiple arguments to pass to a function I prefer using an associative array.

So,

$arraysamp = [‘fruit’=>’apples’, ‘clothing’=>’socks’];

function handleArray($a)
{
switch($a[‘fruit’])
{
case ‘apples’:
// process apples;
return “apple pie”;
break;
default:
return “input argument error”;
break;
}
// other code for processing $a[‘clothing']
}
echo handleArray($arraysamp);

Also, You have declared the global variable in the global script file scope.
As I understand it global is for use inside of a function that elevates the declared
variable to the global scope. Your error may be due to declaring a variable that is
already in the global scope by default, as global.

I understand your concern because I also do a significant amount of javascript programming
and in that language the scope of a function can see a variable in the global scope by default:

var cat = ’siamese’;

function getCat()
{
alert(cat)
}
getCat() // dialog in browser produces “siamese”

My experience and knowledge of php informs me that this is not possible as such.

I hope this can be helpful.
Jeff K


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
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
On Mon, 2018-05-28 at 22:18 -0700, Jeff wrote:
> > On May 28, 2018, at 5:58 PM, John <[email protected]> wrote:
> >
> > On Sun, 2018-05-27 at 19:23 -0700, Jeffry Killen wrote:
> > > On May 27, 2018, at 5:46 PM, 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.
> > > >
> > > > A trivial script illustrating the problem is below.
> > > >
> > > > This seems odd but I don't see what I am doing wrong. Any
> > > > suggestions?
> > > >
> > > > PHP 5.6.30 called through php-fpm on Apache.
> > > >
> > > > Thanks in advance.
> > > >
> > > > John
> > > > ------------------------------------
> > > > test.php - working version
> > > >
> > > > This variant passes a variable in global scope explicitly as a
> > > > parameter. It
> > > > displays correctly on the browser screen.
> > > >
> > > >
> > > > <?php
> > > >
> > > > $bad_line = "This line should display on screen.";
> > > >
> > > > // sample function that works
> > > > function sample1($bad_line)
> > > > {
> > > > return "\n" . $bad_line . "\n";
> > > > }
> > > >
> > >
> > > This function could also be written
> > > function sample1($a)
> > > {
> > > return "\n" . $a . "\n";
> > > }
> > > > echo sample1($bad_line);
> > >
> > > But it looks like you've answered your own question.
> > > If you wanted to have the function, or another function
> > > alter $bad_line, it would need to be passed by reference
> > >
> > > echo sample1(&$bad_line) .... as I understand it.
> > >
> > > > // this call does work
> > > > echo sample1($bad_line);
> > > >
> > > > exit;
> > > > ?>
> > > >
> > > >
> > > >
> > > > test.php - doesn't work
> > > >
> > > > This variant does not work; it throws error:
> > > >
> > > > [Sun May 27 20:17:46.779348 2018] [proxy_fcgi:error] [pid 893:tid
> > > > 139838497978112] [client 192.168.1.104:37732] AH01071: Got error
> > > > 'PHP message:
> > > > PHP Notice: Undefined variable: bad_line in /httpd/iliffe/yrarc/
> > > > test.php on
> > > > line 8\n'
> > > >
> > > > <?php
> > > >
> > > > $bad_line = "This line should display on screen.";
> > > >
> > > > // sample function that doesn't work
> > > > function sample1()
> > > > {
> > > > return "\n" . $bad_line . "\n";
> > > > }
> > > >
> > > > // this call doesn't work
> > > > echo sample1();
> > > >
> > > > exit;
> > > >
> > > > ?>
> > > >
> > > >
> >
> > Thanks Jeffry. Yes, I do have a workaround as you suggested but given the
> > number of variables being passed it is unworkable to have to pass them all
> > as
> > arguments to the function. I have been passing them as an array as I noted
> > in
> > an earlier reply but that is getting unwieldy and will be a maintenance
> > nightmare.
> >
> > The original variables, so far anyway, are not changed within any function
> > but
> > can vary between calls to the various functions. Basically it is running in
> > a
> > loop that sets up the various required variables and then passes everything
> > to
> > one or another function for database processing and/or printing.
> >
> > Thanks for the suggestion though. I'm getting an education here!
> >
> > John
> >
> > >
>
> When I have multiple arguments to pass to a function I prefer using an
> associative array.
>
> So,
>
> $arraysamp = [‘fruit’=>’apples’, ‘clothing’=>’socks’];
>
> function handleArray($a)
> {
> switch($a[‘fruit’])
> {
> case ‘apples’:
> // process apples;
> return “apple pie”;
> break;
> default:
> return “input argument error”;
> break;
> }
> // other code for processing $a[‘clothing']
> }
> echo handleArray($arraysamp);
>
> Also, You have declared the global variable in the global script file scope.
> As I understand it global is for use inside of a function that elevates the
> declared
> variable to the global scope. Your error may be due to declaring a variable
> that is
> already in the global scope by default, as global.
>
> I understand your concern because I also do a significant amount of javascript
> programming
> and in that language the scope of a function can see a variable in the global
> scope by default:
>
> var cat = ’siamese’;
>
> function getCat()
> {
> alert(cat)
> }
> getCat() // dialog in browser produces “siamese”
>
> My experience and knowledge of php informs me that this is not possible as
> such.
>
> I hope this can be helpful.
> Jeff K
>
>
Thanks Jeff. Very helpful, including your comment about "not possible as such".
That was what I was afraid would be the result of this inquiry but I was hoping
I had missed something!

Regards,

John

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On Tue, 2018-05-29 at 13:02 +0200, Christoph M. Becker wrote:
> 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.
>
Thanks for your reply Christoph. From your and other comments it appears that I
can't do what I was trying to do.

On the one hand, it is satisfying that I wasn't making a mistake; on the other,
I sure wish there was a way to avoid passing all these variables in PHP! In a
way it makes the "function" command far less valuable than it should be!

Regards,

John


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On 29.05.2018 at 16:18, John wrote:

> Thanks for your reply Christoph. From your and other comments it appears that I
> can't do what I was trying to do.
>
> On the one hand, it is satisfying that I wasn't making a mistake; on the other,
> I sure wish there was a way to avoid passing all these variables in PHP! In a
> way it makes the "function" command far less valuable than it should be!

On the contrary, I'd say! If I see a variable inside a function body, I
almost immediately know that it is a local variable or a function
parameter (or a superglobal), unless there is a global “declaration”.
Even better, each time I add a global “declaration”, I'm reminded to
think of a better way to express myself, and if I see a lengthy list of
globals, I know that the code will be hard to maintain. See also
http://wiki.c2.com/?GlobalVariablesAreBad.

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On Tue, 2018-05-29 at 22:06 +0200, Christoph M. Becker wrote:
> On 29.05.2018 at 16:18, John wrote:
>
> > Thanks for your reply Christoph. From your and other comments it appears
> > that I
> > can't do what I was trying to do.
> >
> > On the one hand, it is satisfying that I wasn't making a mistake; on the
> > other,
> > I sure wish there was a way to avoid passing all these variables in PHP! In
> > a
> > way it makes the "function" command far less valuable than it should be!
>
> On the contrary, I'd say! If I see a variable inside a function body, I
> almost immediately know that it is a local variable or a function
> parameter (or a superglobal), unless there is a global “declaration”.
> Even better, each time I add a global “declaration”, I'm reminded to
> think of a better way to express myself, and if I see a lengthy list of
> globals, I know that the code will be hard to maintain. See also
> http://wiki.c2.com/?GlobalVariablesAreBad.
>
Well, it's certainly one point of view, and I don't disagree with most of it.

On the other hand, I would assume that the word "global" really means global,
that is, in scope to the entire script, and I think most programming languages
with which I have worked follow this convention.

It is necessary to be aware of the programme structure and define variables in
the proper scope - if the variable is local, then define it in local scope, if
it is required throughout the script or programme, then put it in global scope.

The reference you gave mentions that conflicts could occur with variable names
within a a function but I think this would simply hide the conflicting global
(but I confess to being unsure about that in PHP).

For full disclosure here, I am not a programmer and never have been; simply run
a web site for a small publishing company and I am the sysadmin, web designer,
operator, network specialist and you name it. I worked in IT in operations for
many years on IBM mainframes before I retired; I'm in my late 70's now and all
this is simply to keep me out of trouble! There's nothing on earth more
terrifying that a bored retiree with time on his hands.

Thanks again.

John




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
> On May 29, 2018, at 1:25 PM, John <[email protected]> wrote:
>
> On Tue, 2018-05-29 at 22:06 +0200, Christoph M. Becker wrote:
>> On 29.05.2018 at 16:18, John wrote:
>>
>>> Thanks for your reply Christoph. From your and other comments it appears
>>> that I
>>> can't do what I was trying to do.
>>>
>>> On the one hand, it is satisfying that I wasn't making a mistake; on the
>>> other,
>>> I sure wish there was a way to avoid passing all these variables in PHP! In
>>> a
>>> way it makes the "function" command far less valuable than it should be!
>>
>> On the contrary, I'd say! If I see a variable inside a function body, I
>> almost immediately know that it is a local variable or a function
>> parameter (or a superglobal), unless there is a global “declaration”.
>> Even better, each time I add a global “declaration”, I'm reminded to
>> think of a better way to express myself, and if I see a lengthy list of
>> globals, I know that the code will be hard to maintain. See also
>> http://wiki.c2.com/?GlobalVariablesAreBad.
>>
> Well, it's certainly one point of view, and I don't disagree with most of it.
>
> On the other hand, I would assume that the word "global" really means global,
> that is, in scope to the entire script, and I think most programming languages
> with which I have worked follow this convention.
>
> It is necessary to be aware of the programme structure and define variables in
> the proper scope - if the variable is local, then define it in local scope, if
> it is required throughout the script or programme, then put it in global scope.
>
> The reference you gave mentions that conflicts could occur with variable names
> within a a function but I think this would simply hide the conflicting global
> (but I confess to being unsure about that in PHP).
>
> For full disclosure here, I am not a programmer and never have been; simply run
> a web site for a small publishing company and I am the sysadmin, web designer,
> operator, network specialist and you name it. I worked in IT in operations for
> many years on IBM mainframes before I retired; I'm in my late 70's now and all
> this is simply to keep me out of trouble! There's nothing on earth more
> terrifying that a bored retiree with time on his hands.
>
> Thanks again.
>
> John

Hi again;
We have some things in common, but not including employment.
I am a developer who does this for a hobby and to maintain my own
portfolio site and another for a stained glass artist friend.

This may be off, but as I understand it, in c programming, if you compile a separate
source file with its own globals, those globals aren’t necessarily global to the whole
program in which the compiled source file is included.

But what I do when I have a lot of otherwise globals, as in procedural code, I create
as class definition and create private static variable for reference. These variables
can be initiated and/or configured with the use of a constructor function inside the
class definition. The idea is that global variables are exposed to all sorts of scripts
and functions and can be altered or used by functions and code that shouldn’t be
using and/or altering them. But, I have been programming self processing html/php
files that present and manage user interface elements. So one class definition can
be used to service all requests, for the most part. The class definition, can in turn,
include or require other script files to supplement. These supplemental files can
contain “global” variable definitions with default initialization. These cannot be altered
accept through brute force rewrite of the script file they are defined in.

http://php.net/manual/en/book.classobj.php

I hope this is useful.
Jeff K


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On Tue, 2018-05-29 at 16:38 -0700, Jeff wrote:
> > On May 29, 2018, at 1:25 PM, John <[email protected]> wrote:
> >
> > On Tue, 2018-05-29 at 22:06 +0200, Christoph M. Becker wrote:
> > > On 29.05.2018 at 16:18, John wrote:
> > >
> > > > Thanks for your reply Christoph. From your and other comments it
> > > > appears
> > > > that I
> > > > can't do what I was trying to do.
> > > >
> > > > On the one hand, it is satisfying that I wasn't making a mistake; on the
> > > > other,
> > > > I sure wish there was a way to avoid passing all these variables in
> > > > PHP! In
> > > > a
> > > > way it makes the "function" command far less valuable than it should be!
> > >
> > > On the contrary, I'd say! If I see a variable inside a function body, I
> > > almost immediately know that it is a local variable or a function
> > > parameter (or a superglobal), unless there is a global “declaration”.
> > > Even better, each time I add a global “declaration”, I'm reminded to
> > > think of a better way to express myself, and if I see a lengthy list of
> > > globals, I know that the code will be hard to maintain. See also
> > > http://wiki.c2.com/?GlobalVariablesAreBad.
> > >
> >
> > Well, it's certainly one point of view, and I don't disagree with most of
> > it.
> >
> > On the other hand, I would assume that the word "global" really means
> > global,
> > that is, in scope to the entire script, and I think most programming
> > languages
> > with which I have worked follow this convention.
> >
> > It is necessary to be aware of the programme structure and define variables
> > in
> > the proper scope - if the variable is local, then define it in local scope,
> > if
> > it is required throughout the script or programme, then put it in global
> > scope.
> >
> > The reference you gave mentions that conflicts could occur with variable
> > names
> > within a a function but I think this would simply hide the conflicting
> > global
> > (but I confess to being unsure about that in PHP).
> >
> > For full disclosure here, I am not a programmer and never have been; simply
> > run
> > a web site for a small publishing company and I am the sysadmin, web
> > designer,
> > operator, network specialist and you name it. I worked in IT in operations
> > for
> > many years on IBM mainframes before I retired; I'm in my late 70's now and
> > all
> > this is simply to keep me out of trouble! There's nothing on earth more
> > terrifying that a bored retiree with time on his hands.
> >
> > Thanks again.
> >
> > John
>
> Hi again;
> We have some things in common, but not including employment.
> I am a developer who does this for a hobby and to maintain my own
> portfolio site and another for a stained glass artist friend.
>
> This may be off, but as I understand it, in c programming, if you compile a
> separate
> source file with its own globals, those globals aren’t necessarily global to
> the whole
> program in which the compiled source file is included.
>
> But what I do when I have a lot of otherwise globals, as in procedural code, I
> create
> as class definition and create private static variable for reference. These
> variables
> can be initiated and/or configured with the use of a constructor function
> inside the
> class definition. The idea is that global variables are exposed to all sorts
> of scripts
> and functions and can be altered or used by functions and code that shouldn’t
> be
> using and/or altering them. But, I have been programming self processing
> html/php
> files that present and manage user interface elements. So one class definition
> can
> be used to service all requests, for the most part. The class definition, can
> in turn,
> include or require other script files to supplement. These supplemental files
> can
> contain “global” variable definitions with default initialization. These
> cannot be altered
> accept through brute force rewrite of the script file they are defined in..
>
> http://php.net/manual/en/book.classobj.php
>
> I hope this is useful.
> Jeff K
>
Thanks for the suggestion Jeff. It does sound workable in what I am doing here,
which is basically reporting, where the report format varies depending on
characteristics of the data, but the data is being read in a loop from core
database code that is the mainline of the script. Since several different
formats can occur on succeeding pages I wrote a separate function. That is,
read the data, decide the report format, and call the appropriate page routine.

It is going to take a while for me to fully understand the reference that you
sent but it does look like the solution, in this case anyway.

My sincere thanks for your assistance and patience.

John
=========================================

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
> On May 29, 2018, at 5:03 PM, John <[email protected]> wrote:
>
> On Tue, 2018-05-29 at 16:38 -0700, Jeff wrote:
>>> On May 29, 2018, at 1:25 PM, John <[email protected]> wrote:
>>>
>>> On Tue, 2018-05-29 at 22:06 +0200, Christoph M. Becker wrote:
>>>> On 29.05.2018 at 16:18, John wrote:
>>>>
>>>>> Thanks for your reply Christoph. From your and other comments it
>>>>> appears
>>>>> that I
>>>>> can't do what I was trying to do.
>>>>>
>>>>> On the one hand, it is satisfying that I wasn't making a mistake; on the
>>>>> other,
>>>>> I sure wish there was a way to avoid passing all these variables in
>>>>> PHP! In
>>>>> a
>>>>> way it makes the "function" command far less valuable than it should be!
>>>>
>>>> On the contrary, I'd say! If I see a variable inside a function body, I
>>>> almost immediately know that it is a local variable or a function
>>>> parameter (or a superglobal), unless there is a global “declaration”.
>>>> Even better, each time I add a global “declaration”, I'm reminded to
>>>> think of a better way to express myself, and if I see a lengthy list of
>>>> globals, I know that the code will be hard to maintain. See also
>>>> http://wiki.c2.com/?GlobalVariablesAreBad.
>>>>
>>>
>>> Well, it's certainly one point of view, and I don't disagree with most of
>>> it.
>>>
>>> On the other hand, I would assume that the word "global" really means
>>> global,
>>> that is, in scope to the entire script, and I think most programming
>>> languages
>>> with which I have worked follow this convention.
>>>
>>> It is necessary to be aware of the programme structure and define variables
>>> in
>>> the proper scope - if the variable is local, then define it in local scope,
>>> if
>>> it is required throughout the script or programme, then put it in global
>>> scope.
>>>
>>> The reference you gave mentions that conflicts could occur with variable
>>> names
>>> within a a function but I think this would simply hide the conflicting
>>> global
>>> (but I confess to being unsure about that in PHP).
>>>
>>> For full disclosure here, I am not a programmer and never have been; simply
>>> run
>>> a web site for a small publishing company and I am the sysadmin, web
>>> designer,
>>> operator, network specialist and you name it. I worked in IT in operations
>>> for
>>> many years on IBM mainframes before I retired; I'm in my late 70's now and
>>> all
>>> this is simply to keep me out of trouble! There's nothing on earth more
>>> terrifying that a bored retiree with time on his hands.
>>>
>>> Thanks again.
>>>
>>> John
>>
>> Hi again;
>> We have some things in common, but not including employment.
>> I am a developer who does this for a hobby and to maintain my own
>> portfolio site and another for a stained glass artist friend.
>>
>> This may be off, but as I understand it, in c programming, if you compile a
>> separate
>> source file with its own globals, those globals aren’t necessarily global to
>> the whole
>> program in which the compiled source file is included.
>>
>> But what I do when I have a lot of otherwise globals, as in procedural code, I
>> create
>> as class definition and create private static variable for reference. These
>> variables
>> can be initiated and/or configured with the use of a constructor function
>> inside the
>> class definition. The idea is that global variables are exposed to all sorts
>> of scripts
>> and functions and can be altered or used by functions and code that shouldn’t
>> be
>> using and/or altering them. But, I have been programming self processing
>> html/php
>> files that present and manage user interface elements. So one class definition
>> can
>> be used to service all requests, for the most part. The class definition, can
>> in turn,
>> include or require other script files to supplement. These supplemental files
>> can
>> contain “global” variable definitions with default initialization. These
>> cannot be altered
>> accept through brute force rewrite of the script file they are defined in.
>>
>> http://php.net/manual/en/book.classobj.php
>>
>> I hope this is useful.
>> Jeff K
>>
> Thanks for the suggestion Jeff. It does sound workable in what I am doing here,
> which is basically reporting, where the report format varies depending on
> characteristics of the data, but the data is being read in a loop from core
> database code that is the mainline of the script. Since several different
> formats can occur on succeeding pages I wrote a separate function. That is,
> read the data, decide the report format, and call the appropriate page routine.
>
> It is going to take a while for me to fully understand the reference that you
> sent but it does look like the solution, in this case anyway.
>
> My sincere thanks for your assistance and patience.
>
> John
> =========================================

I am glad to be able to share my experience.
Jeff K.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login