Re: Why is variable not in scope?

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

 



Well, based on the code you posted, I can think of two other things to check. These might have been causing it to fail when you were trying to pass the variable as an argument.

You didn't show the actual if statement before the DB query. Maybe the query wasn't running, so $auth_array was never created.

When you're interacting with the database, you should always check each pg_ function's return value for failure. pg_fetch_array will return false when it fails. Maybe $auth_array was 'false'.

On Sun, Jul 28, 2019 at 7:43 AM John <john.iliffe@xxxxxxxxx> wrote:
Yes, I did do that. Was one of the things I tried when I was
attempting to debug it.

Regards

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

On Sat, 2019-07-27 at 22:35 -0600, LinuxManMikeC wrote:
When you were trying to pass it as an argument, did you remember to declare it in the function's argument list?

// You can name your argument variables whatever you like
function nav_list($an_array_arg) {
    // Use $an_array_arg...
}

// Call the function with your array as an argument
nav_list($auth_array);


On Sat, Jul 27, 2019 at 10:20 PM John <john.iliffe@xxxxxxxxx> wrote:
Bingo! That worked properly.

Does lead to a suggestion: mention that in the Function Arguments section of 
the documentation!

Also, for my curiosity (only) why would this not work when I passed 
$auth_array or &$auth_array as an argument to the call? It should have 
been in scope in the main programme so the value should have been 
available, and certainly the address was for a pass-by-reference.

Thanks to all the people who replied to my question, especially for how quickly
you did so. Made my life MUCH easier!

Regards,

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

On Sat, 2019-07-27 at 21:47 -0600, LinuxManMikeC wrote:
Functions don't automatically have access to global variables. Use the 'global' keyword to declare what global variables you're going to use in your function.

function nav_list() {
    global $auth_array;
    // Code...
}

https://www.php.net/manual/en/language.variables.scope.php

On Sat, Jul 27, 2019, 19:47 John <john.iliffe@xxxxxxxxx> wrote:
There is probably another way to do this but I have spent a good few hours
trying to resolve it and I think there is something wrong with the way I
understand the scope of variables in PHP, so an answer would be appreciated.

I have a PHP (7.1.3) programme that opens a database during initialization,
gathers an associative array of variables (pg_fetch_array) and then closes the
database.  The array name is $auth_array.  During actual display of the page the
values of the elements of the array are used to control what is (not) displayed
and lookup of the values is done using a separate function.

At this point I can print_r() the array and it contains what I expect.

In the next line, I call a user-defined function nav_list() where this array is
used in the form $auth_array['column name'].  At this point I get a PHP error
"Got error 'PHP message: PHP Notice:  Undefined variable: auth_array in
/httpd/myprogramme/yrarcex.php on line 74\nPHP message: PHP Notice:  Undefined
variable: auth_array .... .  At this point I cannot print_r the array, the same
undefined variable message appears.

I expected that auth_array would be in global scope.

so I tried calling nav_list() with no arguments, then with the name of the array
as the only argument, and with the address of the array (&$auth_array) and also
with the explicit cast nav_list(array $auth_array) and I always get the same
error message.  (not a data type error as suggested in the docs).

So far as I can see I am following the online documentation at

https://www.php.net/manual/en/functions.arguments.php

exactly.

The definition of nav_list() is;

function nav_list()
{
 if ($auth_array['u_....'] == 't')   <---- this is line 74 as shown in the error
   {
      // display something
   }
}



[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