PHP:从函数内部声明具有全局作用域的变量


PHP: declare variable with global scope from inside a function

可能重复:
在函数中声明全局变量

是否可以从函数内部声明具有全局作用域的变量?

注意:我不想在函数外接收以前声明的变量的值。但是要让函数内部声明的变量值在函数范围之外工作

我有一种方法,如果我在函数中声明了这些变量:

function variables($n){
    $a=1+$n;
    $b="This is number +$n";
}

我可以在功能之外呼应它们:

variables(1);
echo $a;
echo ''n';
echo $b;
2
This is number 1

我知道我可以从函数中返回一个数组,但是。。。我想肯定的是,否则我也可以。

我在这里什么也没看到:http://php.net/manual/en/language.variables.scope.php

谢谢。

您可以在其他地方了解为什么全局变量不好,所以我将坚持这个问题。您可以为此使用global关键字。如果您想从函数内部读取全局,也同样适用。

function variables($n){
    global $a,$b;
    $a=1+$n;
    $b="This is number +$n";
}