Re: Why is variable not in scope?
| From: | LinuxManMikeC | Date: | Sun, 28 Jul 2019 04:35:41 +0000 |
| Subject: | Re: Why is variable not in scope? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326874@lists.php.net to get a copy of this message | ||
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@iliffe.ca> 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@iliffe.ca> 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
> }
> }
>
>
>