php 在函数内声明全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5355644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring a global variable inside a function
提问by hohner
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:
我有两个 PHP 文件。首先,我根据一个$_GET
值设置了一个 cookie ,然后调用一个函数,然后将该值发送到另一个文件。这是我在join.php 中使用的一些代码:
include('inc/processJoin.php');
setcookie("site_Referral", $_GET['rid'], time()+10000);
$joinProc = new processJoin();
$joinProc->grabReferral($_COOKIE["site_Referral"]);
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)然后将这个值(以及其他)发送到其他文件,这些文件将处理数据并将其插入到数据库中。
The problem I'm having is that when the grabReferral()
function in processJoin.phpis 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()
函数被调用时,变量没有在全局范围内定义 - 其他函数似乎无法访问它以发送到其他文件/进程。$referralID
processJoin.php
I've tried this in processJoin.php:
我在processJoin.php 中试过这个:
grabReferral($rid) {
global $ref_id;
$ref_id = $rid;
}
someOtherFunction() {
sendValue($ref_id);
}
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?
但是 someOtherFunction 似乎无法访问或使用该$ref_id
值。我也试过使用define()
无济于事。我究竟做错了什么?
回答by Felix Geenen
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);
}
felix
菲利克斯
回答by Brad Christie
回答by Vin
This is a simple and working code to initialize global variable from a function :
这是从函数初始化全局变量的简单且有效的代码:
function doit()
{
$GLOBALS['val'] = 'bar';
}
doit();
echo $val;
Gives the output as :
给出输出为:
bar
回答by Leo smith
The following works.
以下作品。
<?php
foo();
bar();
function foo()
{
global $jabberwocky;
$jabberwocky="Jabberwocky<br>";
bar();
}
function bar()
{
global $jabberwocky;
echo $jabberwocky;
}
?>
to produce:
生产:
Jabberwocky
Jabberwocky
胡说八道
胡说八道
So it seems that a variable first declared as global inside a function and then initalised inside that function acquires global scope.
因此,似乎首先在函数内部声明为全局变量,然后在该函数内部初始化的变量获得全局作用域。
回答by Matteo Riva
The global
keyword lets you accessa global variable, not create one. Global variables are the ones created in the outermost scope (i.e. not inside a function or class), and are not accessible inside function unless you declare them with global
.
该global
关键字可以访问一个全局变量,而不是创建一个。全局变量是在最外层作用域(即不在函数或类中)创建的变量,除非使用global
.
回答by Carl Schroedl
Disclaimer: none of this code was tested, but it definitely gets the point across.
免责声明:这些代码都没有经过测试,但它绝对能说明问题。
Choose a namefor the variable you want to be available in the global scope. Within the function, assign a value to the nameindex of the $GLOBALS array.
为您希望在全局范围内可用的变量选择一个名称。在函数内,为 $GLOBALS 数组的名称索引赋值。
function my_function(){
//...
$GLOBALS['myGlobalVariable'] = 42; //globalize variable
//...
}
Now when you want to access the variable from code running in the global scope, i.e. NOT within a function, you can simply use $ nameto access it, without referencing the $GLOBALS array.
现在,当您想从在全局范围内运行的代码访问变量时,即不在函数内,您可以简单地使用 $ name来访问它,而无需引用 $GLOBALS 数组。
<?php
//<global scope>
echo $myGlobalVariable; //outputs "42"
//</global scope>
?>
To access your global variable from a non-global scope such as a function or an object, you have two options:
要从非全局范围(例如函数或对象)访问全局变量,您有两个选择:
- Access it through the appropriate index of the $GLOBALS array. Ex:
$GLOBALS['myGlobalVariable']
This takes a long time to type, especially if you need to use the global variable multiple times in your non-global scope. A more concise way is to import your global variable into the local scope by using the 'global' statement. After using this statement, you can reference the global variable as though it were a local variable. Changes you make to the variable will be reflected globally.
//<non global scopes> function a(){ //... global $myGlobalVariable; echo $myGlobalVariable; // outputs "42" //... } function b(){ //... echo $GLOBALS['myGlobalVariable']; // outputs "42" echo $myGlobalVariable; // outputs "" (nothing) // ^also generates warning - variable not defined //... } //</non global scopes>
- 通过 $GLOBALS 数组的适当索引访问它。例如:
$GLOBALS['myGlobalVariable']
这需要很长时间才能输入,特别是如果您需要在非全局范围内多次使用全局变量。 更简洁的方法是使用“global”语句将全局变量导入局部作用域。使用此语句后,您可以像引用局部变量一样引用全局变量。您对变量所做的更改将全局反映。
//<non global scopes> function a(){ //... global $myGlobalVariable; echo $myGlobalVariable; // outputs "42" //... } function b(){ //... echo $GLOBALS['myGlobalVariable']; // outputs "42" echo $myGlobalVariable; // outputs "" (nothing) // ^also generates warning - variable not defined //... } //</non global scopes>
Please use global variables in any language with caution, especially in PHP.
请谨慎使用任何语言的全局变量,尤其是在 PHP 中。
See the following resources for discussion of global variables:
有关全局变量的讨论,请参阅以下资源:
回答by Khachatur Danielyan
I hope that helped
我希望有帮助
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>