問題描述
我有兩個(gè) PHP 文件.首先,我根據(jù) $_GET
值設(shè)置了一個(gè) cookie,然后調(diào)用一個(gè)函數(shù),然后將該值發(fā)送到另一個(gè)文件.這是我在 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"]);
然后另一個(gè)文件 (processJoin.php) 會將此值(以及其他)發(fā)送到其他文件,這些文件將處理數(shù)據(jù)并將其插入到數(shù)據(jù)庫中.
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.
我遇到的問題是,當(dāng) processJoin.php 中的 grabReferral()
函數(shù)被調(diào)用時(shí),$referralID
變量是沒有在全球范圍內(nèi)定義 - processJoin.php
中的其他函數(shù)似乎無法訪問它以發(fā)送到其他文件/進(jìn)程.
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 中試過這個(gè):
grabReferral($rid) {
global $ref_id;
$ref_id = $rid;
}
someOtherFunction() {
sendValue($ref_id);
}
但是 someOtherFunction 似乎無法訪問或使用 $ref_id
值.我也試過使用 define()
無濟(jì)于事.我做錯(cuò)了什么?
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?
推薦答案
你也必須在第二個(gè)函數(shù)中定義全局變量..
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);
}
菲利克斯
這篇關(guān)于在函數(shù)內(nèi)聲明全局變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!