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

>Up until now this has worked fine. But now I have a function in the
> file that tries to read the value of a cookie called
>"testcookie". My problem is that the 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

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"

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

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

The new-fangled "superglobals" $_POST, $_GET, $_COOKIE etc are global

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?

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to