問題描述
我有兩個 PHP 文件.首先,我根據 $_GET
值設置了一個 cookie,然后調用一個函數,然后將該值發送到另一個文件.這是我在 join.php 中使用的一些代碼:
I have two PHP files. In the first I set a cookie based on a $_GET
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:
include('inc/processJoin.php');
setcookie("site_Referral", $_GET['rid'], time()+10000);
$joinProc = new processJoin();
$joinProc->grabReferral($_COOKIE["site_Referral"]);
然后另一個文件 (processJoin.php) 會將此值(以及其他)發送到其他文件,這些文件將處理數據并將其插入到數據庫中.
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.
我遇到的問題是,當 processJoin.php 中的 grabReferral()
函數被調用時,$referralID
變量是沒有在全球范圍內定義 - processJoin.php
中的其他函數似乎無法訪問它以發送到其他文件/進程.
The problem I'm having is that when the grabReferral()
function in processJoin.php is called, the $referralID
variable isn't being defined on a global scale - other functions in processJoin.php
can't seem to access it to send to other files/processes.
我在 processJoin.php 中試過這個:
grabReferral($rid) {
global $ref_id;
$ref_id = $rid;
}
someOtherFunction() {
sendValue($ref_id);
}
但是 someOtherFunction 似乎無法訪問或使用 $ref_id
值.我也試過使用 define()
無濟于事.我做錯了什么?
But the someOtherFunction can't seem to access or use the $ref_id
value. I've also tried using define()
to no avail. What am I doing wrong?
推薦答案
你也必須在第二個函數中定義全局變量..
you have to define the global var in the second function as well..
// global scope
$ref_id = 1;
grabReferral($rid){
global $ref_id;
$ref_id = $rid;
}
someOtherFunction(){
global $ref_id;
sendValue($ref_id);
}
菲利克斯
這篇關于在函數內聲明全局變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!