hohner hohner - 4 years ago 177
PHP Question

Declaring a global variable inside a function

I have two PHP files. In the first I set a cookie based on a

value, and then call a function which then sends this value on to the other file. This is some code which I'm using in join.php:

setcookie("site_Referral", $_GET['rid'], time()+10000);
$joinProc = new processJoin();

The other file (processJoin.php) will then send this value (among others) to further files which will process and insert the data into the database.

The problem I'm having is that when the
function in processJoin.php is called, the
variable isn't being defined on a global scale - other functions in
can't seem to access it to send to other files/processes.

I've tried this in processJoin.php:

grabReferral($rid) {
global $ref_id;
$ref_id = $rid;

someOtherFunction() {

But the someOtherFunction can't seem to access or use the
value. I've also tried using
to no avail. What am I doing wrong?

Answer Source

you have to define the global var in the second function as well..

$ref_id = 1;

   global $ref_id;
   $ref_id = $rid;

    global $ref_id;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download