Re: How Do I make Global Scope Variables Available to Functions
| From: | Jeff | Date: | Tue, 29 May 2018 05:18:40 +0000 |
| Subject: | Re: How Do I make Global Scope Variables Available to Functions | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326492@lists.php.net to get a copy of this message | ||
> On May 28, 2018, at 5:58 PM, John <john.iliffe@iliffe.ca> 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