在函数内声明全局变量

时间:2022-05-02 00:41:46

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.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()函数时,$ 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()无济于事。我究竟做错了什么?

7 个解决方案

#1


27  

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

#2


21  

personally, I would recommend the $GLOBALS super variable.

就个人而言,我会推荐$ GLOBALS超级变量。

function foo(){
  $GLOBALS['foobar'] = 'foobar';
}
function bar(){
  echo $GLOBALS['foobar'];
}
foo();
bar();

DEMO

#3


9  

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

#4


2  

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.

因此,似乎变量首先在函数内声明为全局变量,然后在该函数内部初始化,获取全局范围。

#5


1  

The global keyword lets you access a 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关键字允许您访问全局变量,而不是创建全局变量。全局变量是在最外层范围内创建的变量(即不在函数或类中),并且除非使用全局变量声明它们,否则不能在函数内部访问。

#6


1  

Disclaimer: none of this code was tested, but it definitely gets the point across.

免责声明:这些代码都没有经过测试,但绝对可以解决问题。

Choose a name for the variable you want to be available in the global scope. Within the function, assign a value to the name index 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 $ name to 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:

要从非全局范围(如函数或对象)访问全局变量,您有两个选择:

  1. 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.
  2. 通过$ GLOBALS数组的相应索引访问它。例如:$ GLOBALS ['myGlobalVariable']这需要很长时间才能输入,特别是如果你需要在非全局范围内多次使用全局变量。

  3. 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.

    更简洁的方法是使用'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:

有关全局变量的讨论,请参阅以下资源:

#7


0  

The visibility of a variable

变量的可见性

I hope that helped

我希望有所帮助

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;
?>

#1


27  

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

#2


21  

personally, I would recommend the $GLOBALS super variable.

就个人而言,我会推荐$ GLOBALS超级变量。

function foo(){
  $GLOBALS['foobar'] = 'foobar';
}
function bar(){
  echo $GLOBALS['foobar'];
}
foo();
bar();

DEMO

#3


9  

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

#4


2  

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.

因此,似乎变量首先在函数内声明为全局变量,然后在该函数内部初始化,获取全局范围。

#5


1  

The global keyword lets you access a 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关键字允许您访问全局变量,而不是创建全局变量。全局变量是在最外层范围内创建的变量(即不在函数或类中),并且除非使用全局变量声明它们,否则不能在函数内部访问。

#6


1  

Disclaimer: none of this code was tested, but it definitely gets the point across.

免责声明:这些代码都没有经过测试,但绝对可以解决问题。

Choose a name for the variable you want to be available in the global scope. Within the function, assign a value to the name index 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 $ name to 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:

要从非全局范围(如函数或对象)访问全局变量,您有两个选择:

  1. 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.
  2. 通过$ GLOBALS数组的相应索引访问它。例如:$ GLOBALS ['myGlobalVariable']这需要很长时间才能输入,特别是如果你需要在非全局范围内多次使用全局变量。

  3. 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.

    更简洁的方法是使用'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:

有关全局变量的讨论,请参阅以下资源:

#7


0  

The visibility of a variable

变量的可见性

I hope that helped

我希望有所帮助

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;
?>