php 全局变量修饰符不起作用

发布于 2024-07-18 03:27:08 字数 230 浏览 5 评论 0原文

我使用基本的 php 示例作为全局修饰符,但它对我不起作用:-|

$a = 1;
$b = 2;

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

    $b = $a + $b;
} 

Sum();
echo "***: ".$b;

这是结果... $ ***: 2

php.ini 上是否有任何参数可能会影响此问题?

I'm using the basic php example for the global modifier, and it doesn't work for me :-|

$a = 1;
$b = 2;

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

    $b = $a + $b;
} 

Sum();
echo "***: ".$b;

Here is the result...
$ ***: 2

Is there any parameter on the php.ini that might effect this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

悲喜皆因你 2024-07-25 03:27:08

我也面临着你的问题。 由于我使用的是框架 (Yii),我并不完全意识到我的代码确实嵌套在函数内,因此 global 的行为不符合预期(如 omadmedia 和其他人所解释的) 。

我的解决方案非常简单:

global $a;
global $b;
$a = 1;
$b = 2;

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

    $b = $a + $b;
} 

Sum();
echo "***: ".$b;

I was also faced with your problem. As I am using a framework (Yii), I wasn't exactly aware that my code was indeed nested inside functions and, therefore, global wasn't behaving as expected (as explained by omadmedia and others).

My solution is pretty simple:

global $a;
global $b;
$a = 1;
$b = 2;

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

    $b = $a + $b;
} 

Sum();
echo "***: ".$b;
烟柳画桥 2024-07-25 03:27:08

不管你信不信,我也得到答案:2。
这意味着确实存在一些全局不起作用的情况。

尝试查找原因:
看来如果你有一个函数并将OP的代码(这是一个php.net示例)放入该函数中,你将得到答案2。
这有点奇怪,但在某种程度上有点道理......

(我在Win XP中的Apache 2.2.8下使用PHP 5.2.5)

LE:
我的解决方案
好的,解决了这个问题:当您在第二个函数中使用 global 时,您显然会获得超全局变量,这些变量可供每个人使用(即在任何函数外部声明),但由于 $a 和 $b 是在第一个函数内声明的,因此它们不是该范围的一部分,并且不可用于第二个功能。
我对解决方案的猜测是将 $a 和 $b 声明为全局的,也在第二个函数之外,即在第一个函数内部。
!! 请注意,由于各种原因,第一个可能不是那么明显,例如您的文件(仅包含第二个函数)被包含在不同文件中不同函数的主体中的某处。

Believe it or not, I get answer: 2 as well.
This means there are indeed some cases where global is not working.

Tried finding the cause:
It seems that if you have a function and put the OP's code (which is a php.net example) inside that function, you will get answer 2.
This is a bit weird and kinda makes sense in a way...

(I'm using PHP 5.2.5 under Apache 2.2.8 in Win XP)

LE:
MY SOLUTION
OK, solved this: when you use global in the 2nd function you obviously get the superglobal variables, those available to everybody (ie. decalared outside any function), but since $a and $b are declared inside the 1st function, they are not part of that scope and are not available to the 2nd function.
My guess for a solution is to declare $a and $b global, outside the 2nd function as well, that is inside the 1st function.
!! Note that the 1st may be not so obvious due to various reasons, like your file (only containing the 2nd function) being included somewhere in the body of a different function in a different file.

浅听莫相离 2024-07-25 03:27:08

正如@AgelessEssence 回答的那样,如果您有嵌套函数,则全局关键字不起作用。 从他的例子中可以看出这一点。 但是,如果不清楚是否包含文件。 这是一个例子。

//a.php
function f() {
    require_once('./a_inc.php');
}

f();

//a_inc.php
$a = 12;

function g() {
    global $a;

    var_dump($a);
}

g();

//result
null

在上面的代码中,$a 看起来像一个全局变量。 其实不是因为它包含在a.php中的函数f()中,而$a是函数f()的一部分。

因此,当您的全局关键字不起作用时,请检查它是否包含在函数中。 由于这个问题的解决方案在其他答案中已经解释得很清楚了,所以我没有在这里添加它。

As @AgelessEssence answered, global keyword doesn't work if you have a nested function. It is obvious in his example. However, if it is not clear if a file is included. Here is the example.

//a.php
function f() {
    require_once('./a_inc.php');
}

f();

//a_inc.php
$a = 12;

function g() {
    global $a;

    var_dump($a);
}

g();

//result
null

In the code above, $a looks like a global variable. Actually, it is not because it is included in the function f() in a.php and $a is part of function f().

So, when your global keyword doesn't work, check whether it is included in a function. As the solution for this problem well explained in other answers, so I didn't add it here.

清泪尽 2024-07-25 03:27:08

我和你一样有同样的问题,终于找到了答案

工作代码 / DEMO

$a=1;

function showA() {

    global $a;
    var_export($a);  

} 

showA(); // outputs "1"

非工作代码 / DEMO

function encapsulation() {

    $a=1;

    function showA() {

        global $a;
        var_export($a);  

    };

    showA();

}  

encapsulation(); // outputs "NULL"

如您所见,问题在嵌套函数定义中使用全局关键字时会发生

更多信息:php.net/manual/en/language.variables.scope.php#92868

i have the SAME PROBLEM like you, and finally found the answer

working code / DEMO

$a=1;

function showA() {

    global $a;
    var_export($a);  

} 

showA(); // outputs "1"

non working code / DEMO

function encapsulation() {

    $a=1;

    function showA() {

        global $a;
        var_export($a);  

    };

    showA();

}  

encapsulation(); // outputs "NULL"

as you can see, the problem occurs when using the global keyword inside a nested function definition

More Info: php.net/manual/en/language.variables.scope.php#92868

屋顶上的小猫咪 2024-07-25 03:27:08

您上面的示例代码对我有用。 但您也可以使用 $GLOBALS 超变量。

function Sum()
{
    $a = $GLOBALS["a"];
    $b =& $GLOBALS["b"];
    $b = $a + $b;
} 

如果可以的话,就不应该使用全局变量。 有更好的方法来创建你的函数。 使用参数(参数)(也许经过 参考) 和 返回一个值 来代替。

/**
 * Calculate the sum of the parameters
 * @param int|float $a one or more parameter
 * @param int|float $a, ... 
 * @return int|float
 */
function sum($a)
{
    $args = func_get_args();
    return array_sum($args);
}

$a = 1;
$b = 2;

$b = sum($a, $b);

使用 PHPDOC,您可以了解您的函数在几年后的用途无需阅读代码。 使用一个好的IDE,您还可以在编写函数时获得解释和参数顺序。

Your example code above works for me. But you can also use the $GLOBALS supervariable.

function Sum()
{
    $a = $GLOBALS["a"];
    $b =& $GLOBALS["b"];
    $b = $a + $b;
} 

Global variables should not be used if you can help it though. There are better ways to make your functions. Use parameters (arguments) (maybe pass by reference) and return a value instead.

/**
 * Calculate the sum of the parameters
 * @param int|float $a one or more parameter
 * @param int|float $a, ... 
 * @return int|float
 */
function sum($a)
{
    $args = func_get_args();
    return array_sum($args);
}

$a = 1;
$b = 2;

$b = sum($a, $b);

With PHPDOC you can understand what your functions do years from now without reading the code. With a good IDE you can also get the explanation and argument order as you write the function.

乄_柒ぐ汐 2024-07-25 03:27:08

我唯一能想象出的错误是,如果您在先调用函数后在全局范围内分配变量。 也就是说,您的函数实际上是在声明变量,然后您只需在其他地方覆盖它们即可。 例如,调用 Sum()then 执行 $a=1$b=2

The only thing I could imagine going wrong is if you're assigning variables in the global scope after calling a function first. That is, your function is actually declaring the variables and then you just overwrite them elsewhere. For example, calling Sum() and then doing $a=1, $b=2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文