[Fwd: Re: Why is variable not in scope?]

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

 



Sorry, forgot to reply all

John
-------------------------------

-------- Forwarded Message --------
From: John <john.iliffe@xxxxxxxxx>
Reply-To: john.iliffe@xxxxxxxxx
To: Sam Hobbs <Sam@xxxxxxxxxxxxxxxxxx>
Subject: Re: Why is variable not in scope?
Date: Sun, 28 Jul 2019 00:03:45 -0400

> Thanks for the prompt response Sam. Much appreciated.
> 
> Here is all of the relevant part of the code. (I start part way through
> because this is part of the
> security on this web site. This snippet is included in its entirety in all of
> the screens). I have 
> annotated what is going on.
> 
> Comments starting with // below are part if code; annotation for this memo
> starts with <---
> 
> // Screen programme is starting here, (<body>) this varies for each screen
> // 27 lines of php variable definitions; none related to the problem at hand.
> 
> 
> <----- objective is to get a global scope for $auth_array
> $auth_array = ""; <--- I have tried various things with this variable,
> including
> not having it at all, setting it to = new array;, leaving it uninitialized,
> etc.
> 
> 
> 
> include_once my_programme.php <--- this is in each screen that needs security
> if not required, $auth_array is never referenced in definitions
> 
> <---- some html that is peculiar to each screen, no php code at this point
> 
> // myprogramme.php <--- all of the following is what gets included
> <--- the included code contains some instream php that
> is effectively part of the main programme and one
> function, nav_list()
> 
> --------
> <----irrelevant common initialization here used by all screens
> this is effectively inline with the main programme
> .........
> 
> //  logged in OK so get authorizations
> 
> <--- start by reading database and getting various permissions
>    $_query = "SELECT
> u_read_view,u_upd_user,u_upd_prop,u_upd_rptr,u_upd_mbr,u_upd_dues FROM y_uids
> WHERE u_uid='" . $uid . "';";
>    $_dbhandle = pg_connect('dbname=XXXX user=YYYY password=PASSWORD');
>    $_result = pg_query($_dbhandle, $_query);
>    $auth_array = pg_fetch_array($_result,0,PGSQL_ASSOC); <--- auth_array is
> created here
>    pg_close($_dbhandle);        // avoid excessive db connections
> 
> print_r($auth_array); <---- this works and prints what I expected to see
>   }
> else                    // not logged in; no cookie
> {
> ... error handling
> }
> 
> // create navigation based on authorities
> function nav_list() <---this is function where the error occurs ****
> {
> $arrx = print_r($auth_array); <--- this fails with undefined variable message
> echo "<br /> <br />" . $arrx . "<br /> <br />";
> 
>  if ($auth_array['u_read_view'] == 't') <---- this is the "line 74" in the
> error message
> {
> echo "XXXXXXX";
> }
>  if ($auth_array['u_upd_user'] == 't')
> {
> echo "ZZZZZZZ";
> }
> }
> .......
> <---- doesn't ever get past here but several lines similar to above and then
> end of function
> <---- end of function nav_list()
> 
> main programme resumes here
> ..... some html, then:
> 
> <?php nav_list() ?> <---- set up the appropriate navigation for the user's
> auths
> 
> ...... more html and php
> 
> **** I have tried the call in many ways, including
> <?php nav_list() ?>
> <?php nav_list($auth_array) ?>
> <?php nav_list(&$auth_array) ?>
> 
> 
> ----------------------------------------------
> On Sat, 2019-07-27 at 18:57 -0700, Sam Hobbs wrote:
> > You probably need to show more code. If you can show how/where auth_array is
> > defined and how nav_list is called then that would help.
> > 
> > The best way to get help is to create a complete small sample that re-
> > creates the problem. That might seem like a lot of work but I have done it.
> > Sometimes I solve the problem myself when I do that.
> > 
> > > John Saturday, July 27, 2019 6:46 PM
> > > 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