全局变量不能像框架那样按预期工作 [英] Global variables aren't working as expected with frameworks

查看:91
本文介绍了全局变量不能像框架那样按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的php脚本中使用全局变量,并且在 php.net 。但它在我的本地服务器上不起作用。有没有我错过的配置?



那个页面

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

函数Sum()
{
global $ a,$ b;

$ b = $ a + $ b;
}

Sum();
echo $ b;
?>

以上脚本将输出3。



然而,我的输出是2!



另一个例子:

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

函数Sum()
{
$ GLOBALS ['b'] = $ GLOBALS ['a'] + $ GLOBALS ['b'];
}

Sum();
echo $ b;
?>

我得到错误:


未定义的指数:a


那么这是怎么回事呢?为什么我的代码不能按预期工作?

按照他们的方式,我使用的是Laravel。

解决方案

所以我猜你正在使用一个框架,如你所说在评论


@ Rizier123 是的,我正在使用Laravel 。有关系吗? - 凯6分钟前


如果它很重要?是的它的确如此。



可能发生的情况是,你在这里展示的代码被包装到其他地方的另一个函数中。

意味着 Sum()函数中的变量在全局范围内,但其他变量不在其中,因为它们可能在另一个范围内函数==另一个作用域。






您可以使用以下代码重现它:

 函数anotherFunction(){
$ a = 1;
$ b = 2;

函数Sum(){
$ GLOBALS ['b'] = $ GLOBALS ['a'] + $ GLOBALS ['b'];
}

Sum();
echo $ b;
}

anotherFunction();

如果您有


function.error-reporting.phprel =nofollow noreferrer> error reporting on you will get: >注意:未定义的索引:a

注意:未定义的索引:b

2




只需将错误报告放在文件的顶部即可获取有用的错误消息:

 <?php 
ini_set(display_errors,1);
error_reporting(E_ALL);
?>

现在要解决这个问题,您必须在全局范围内声明变量,或者使用:

  $ GLOBALS [a] = 1; 
$ GLOBALS [b] = 2;

或者像这样:

 全局$ a,$ b; 
$ a = 1;
$ b = 2;


I'm trying to use global variables in my php script, and I found the usage of global variable on php.net. But it doesn't work on my local server. Is there any configuration I have missed out?

There is an example found on that page:

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

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

    $b = $a + $b;
} 

Sum();
echo $b;
?>

The above script will output 3.

However my output is 2!

Another example:

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

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

Sum();
echo $b;
?>

I get error:

Undefined index: a

So what is the deal with that? Why doesn't my code work as expected?
By they way, I'm using Laravel.

解决方案

So as I guessed you are using a framework as you said in the comments:

@Rizier123 Yes, I'm using Laravel. Does it matter? – Kai 6 mins ago

And if it matters? Yes it does.

Probably what is happening here is, that the code which you show us here is wrapped into another function somewhere else.

Means that the variables in the Sum() function are in global scope, but the other ones outside of it not, since they are probably in another function == another scope.


You can reproduce it with this code:

function anotherFunction() {
    $a = 1;
    $b = 2;

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

    Sum();
    echo $b;
}

anotherFunction();

And if you have error reporting on you will get:

Notice: Undefined index: a
Notice: Undefined index: b
2

Just put the error reporting at the top of your file(s) to get useful error messages:

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>

To solve this now you have to declare the variables also in global scope, either with:

$GLOBALS["a"] = 1;
$GLOBALS["b"] = 2;

or like this:

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

这篇关于全局变量不能像框架那样按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆