PHP在函数中访问全局变量

时间:2022-09-21 00:41:26

According to the most programming languages scope rules , i can access variables that defined outside of functions inside them , but why this code doesn't work?

根据大多数编程语言范围规则,我可以访问在其中的函数之外定义的变量,但为什么这段代码不起作用?

<?php
$data='My data';
function menugen(){

    echo "[".$data."]";
}
menugen();
?>

There is [] in output.

输出中有[]。

6 个解决方案

#1


96  

It is not working because you have to declare which global variables you'll be accessing:

它无法正常工作,因为您必须声明要访问的全局变量:

$data='My data';
function menugen(){
    global $data; // <-- add this line
    echo "[".$data."]";
}
menugen();

otherwise you can access it as $GLOBALS['data'], see http://php.net/manual/en/language.variables.scope.php

否则您可以将其作为$ GLOBALS ['data']访问,请参阅http://php.net/manual/en/language.variables.scope.php

Even if a little OT, I'd suggest you avoid using globals at all and prefer passing as parameters.

即使有点OT,我建议你完全避免使用全局变量而更喜欢传递参数。

#2


8  

You can do one of the following:

您可以执行以下操作之一:

<?php
$data='My data';
function menugen(){
    global $data;
    echo "[".$data."]";
}
menugen();

Or

要么

<?php
$data='My data';
function menugen(){
    echo "[".$GLOBALS['data']."]";
}
menugen();

That being said, overuse of globals can lead to some poor code. It is usually better to pass in what you need. For example instead of referencing a global database object you should pass in a handle to the database and act upon that. This is called Dependency Injection. It makes your life a lot easier when you implement automated testing (which you should).

话虽这么说,过度使用全局变量可能导致一些糟糕的代码。传递你需要的东西通常更好。例如,您应该将句柄传递给数据库并对其进行操作,而不是引用全局数据库对象。这称为依赖注入。当您实施自动化测试时,它会让您的生活变得更轻松(您应该这样做)。

#3


6  

It's a matter of scope. In short, Global variables should be avoided SO:

这是一个范围问题。简而言之,应该避免使用全局变量:

You either need to pass it as a parameter:

您需要将其作为参数传递:

$data = 'My data';

function menugen($data)
{
    echo $data;
}

OR have it in a class and access it

或者将它放在一个类中并访问它

class MyClass
{
    private $data = "";

    function menugen()
    {
        echo this->data;
    }

}

Edit: See @MatteoTassinari answer as well as you can mark it as global to access it but global vars are generally not required so it would be wise to re-think your coding.

编辑:请参阅@MatteoTassinari答案以及您可以将其标记为全局访问它,但通常不需要全局变量,因此重新考虑您的编码是明智的。

#4


4  

Another way to do it:

另一种方法:

<?php

$data = 'My data';

$menugen = function() use ($data) {

    echo "[".$data."]";
};

$menugen();

#5


0  

You need to pass the variable into the function:

您需要将变量传递给函数:

$data = 'My data';

function menugen($data)
{
    echo $data;
}

#6


-1  

If you want you can use "define" function but this function create a constants which can't be changed once defined.

如果需要,可以使用“define”函数,但此函数会创建一个定义后无法更改的常量。

<?php
define("GREETING", "Welcome to W3Schools.com!");

function myTest() {
    echo GREETING;
}

myTest();
?>

http://www.w3schools.com/php/php_constants.asp

http://www.w3schools.com/php/php_constants.asp

#1


96  

It is not working because you have to declare which global variables you'll be accessing:

它无法正常工作,因为您必须声明要访问的全局变量:

$data='My data';
function menugen(){
    global $data; // <-- add this line
    echo "[".$data."]";
}
menugen();

otherwise you can access it as $GLOBALS['data'], see http://php.net/manual/en/language.variables.scope.php

否则您可以将其作为$ GLOBALS ['data']访问,请参阅http://php.net/manual/en/language.variables.scope.php

Even if a little OT, I'd suggest you avoid using globals at all and prefer passing as parameters.

即使有点OT,我建议你完全避免使用全局变量而更喜欢传递参数。

#2


8  

You can do one of the following:

您可以执行以下操作之一:

<?php
$data='My data';
function menugen(){
    global $data;
    echo "[".$data."]";
}
menugen();

Or

要么

<?php
$data='My data';
function menugen(){
    echo "[".$GLOBALS['data']."]";
}
menugen();

That being said, overuse of globals can lead to some poor code. It is usually better to pass in what you need. For example instead of referencing a global database object you should pass in a handle to the database and act upon that. This is called Dependency Injection. It makes your life a lot easier when you implement automated testing (which you should).

话虽这么说,过度使用全局变量可能导致一些糟糕的代码。传递你需要的东西通常更好。例如,您应该将句柄传递给数据库并对其进行操作,而不是引用全局数据库对象。这称为依赖注入。当您实施自动化测试时,它会让您的生活变得更轻松(您应该这样做)。

#3


6  

It's a matter of scope. In short, Global variables should be avoided SO:

这是一个范围问题。简而言之,应该避免使用全局变量:

You either need to pass it as a parameter:

您需要将其作为参数传递:

$data = 'My data';

function menugen($data)
{
    echo $data;
}

OR have it in a class and access it

或者将它放在一个类中并访问它

class MyClass
{
    private $data = "";

    function menugen()
    {
        echo this->data;
    }

}

Edit: See @MatteoTassinari answer as well as you can mark it as global to access it but global vars are generally not required so it would be wise to re-think your coding.

编辑:请参阅@MatteoTassinari答案以及您可以将其标记为全局访问它,但通常不需要全局变量,因此重新考虑您的编码是明智的。

#4


4  

Another way to do it:

另一种方法:

<?php

$data = 'My data';

$menugen = function() use ($data) {

    echo "[".$data."]";
};

$menugen();

#5


0  

You need to pass the variable into the function:

您需要将变量传递给函数:

$data = 'My data';

function menugen($data)
{
    echo $data;
}

#6


-1  

If you want you can use "define" function but this function create a constants which can't be changed once defined.

如果需要,可以使用“define”函数,但此函数会创建一个定义后无法更改的常量。

<?php
define("GREETING", "Welcome to W3Schools.com!");

function myTest() {
    echo GREETING;
}

myTest();
?>

http://www.w3schools.com/php/php_constants.asp

http://www.w3schools.com/php/php_constants.asp