In article <[EMAIL PROTECTED]> ,
[EMAIL PROTECTED] (Chris Morrow) wrote:

>Up until now this has worked fine. But now I have a function in the
>settings.inc file that tries to read the value of a cookie called
>"testcookie". My problem is that the settings.inc file doesn't seem to be
>able to access the cookie at all. But if I try to access it direct from a
>page rather than the required file it works fine.

It has nothing to do with the require and *everything* to do with the
FUNCTION.

You see, it's a really Bad Idea (tm) to have your functions use variables
from the "outside" world -- variables should be passed in as parameters.

function foo (parameter, parameter, ..., parameter){
.
.
.
}

Inside the function "foo" there are *NO* variables except the "parameter"
variables.

You can over-ride this, though, using "global"

function foo (parameter, parameter, ..., parameter){
  global $cookie1;
.
.
.
}

EXCEPTION:
The new-fangled "superglobals" $_POST, $_GET, $_COOKIE etc are global
everywhere.

So you *could* use $_COOKIE['cookie1'] without using "global".

This rule about variable scope is a FEATURE -- Without it, the function is a
relatively weak little thing, and you are all to likely to have stupid,
stupid, stupid bugs from variable name conflicts and a million lines of PHP
code on your web-server to find the mistake...  With the rule that only the
paramters (and globals) can exist inside a function, you limit your problem
search to a very much smaller space.
-- 
Like Music?  http://l-i-e.com/artists.htm


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to