我可以在 PHP 中的不同函数之间共享变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3898170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:24:23  来源:igfitidea点击:

Can I share variables between different functions in PHP?

phpvariablesfunction

提问by aborted

I'll try to explain with an example...

我会试着用一个例子来解释......

Let's say I have two different functions, and one of them has a defined variable. In the second function, I don't wanna write the same variable again, can I simply use the variable from the first function in the second one WITHOUT redefining it in the second function?

假设我有两个不同的函数,其中之一有一个定义的变量。在第二个函数中,我不想再次编写相同的变量,我可以简单地在第二个函数中使用第一个函数中的变量而不在第二个函数中重新定义它吗?

Something like:

就像是:

function a()
{
  $var = "my variable";
}

function b()
{
 echo $var;
}

Sorry if this questions is a bit silly, but I'm still a beginner =).

对不起,如果这个问题有点傻,但我仍然是初学者=)。

采纳答案by Peter Ajtai

Sure you can do globals, but of PHP 5.3.0+ has anonymous functions, so you can also do closures:

当然你可以做全局变量,但是 PHP 5.3.0+ 有匿名函数,所以你也可以做闭包:

<?php

function a(){
  $a = 1;  
  echo "First: $a, ";
  ++$a;

    // This is a closure. It can gain access to the variables of a() with the 
    // use option.
  $b = function() use ($a) {
      echo "second: $a";  
  };  
  $b();
};
a(); // Outputs: First: 1, second: 2
?>

Try it out at this Codepad example

在此键盘示例中尝试一下


or probably more useful:


或者可能更有用:

<?php

function a(){
  $a = 1;  
  echo "First: $a, ";
  ++$a;
  $b = function() use ($a) {
      echo "second: $a";  
  };        
  return $b;
};
$fun = a();     // $fun is now $b & $b has access to $a!
$fun();
// Outputs: First: 1, second: 2
?>

Try it out at this Codepad example

在此键盘示例中尝试一下

From the docs:

从文档:

Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from).

闭包也可以从父作用域继承变量。任何此类变量都必须在函数头中声明。从父作用域继承变量与使用全局变量不同。全局变量存在于全局作用域中,无论执行什么函数都是一样的。闭包的父作用域是声明闭包的函数(不一定是调用它的函数)。

回答by lonesomeday

The cleanest solution here is to make A and B methods of a class and have the common variables as private static variables. This removes the messiness of globals and allows both functions to be globally accessible.

这里最干净的解决方案是创建一个类的 A 和 B 方法,并将公共变量作为私有静态变量。这消除了全局变量的混乱,并允许全局访问这两个函数。

class SomeClass
{
    private static $a = 1;

    public static function a() {
        self::$a = 2;
    }

    public static function b() {
        echo self::$a;
    }
}

You can then call the functions:

然后你可以调用函数:

SomeClass::a();
SomeClass::b();

回答by fredley

$a = 1;
function a(){
  global $a;
  $a = 2;
  }
function b(){
  global $a;
  echo $a;
  }

a();
b();

output:

输出:

2

回答by Pavnish Yadav

Like this . you can use

像这样 。您可以使用

function a()
{
  global $var; 
  $var = "my variable";
}

function b()
{
 global $var; 
 echo $var;
}

回答by Marcus Adams

I'd do it like this. This is a more common implementation. Remember that functions can be passed parameters and can return values.

我会这样做。这是一个更常见的实现。请记住,函数可以传递参数,也可以返回值。

// Main
$a = a();
b($a);

function a()
{
  return "my variable";
}

function b($a)
{
 echo $a;
}

You have the main body of a program, and the variable $aremains in scope there. Globals are rarely used, and technically never needed.

你有一个程序的主体,变量$a仍然在那里。全局变量很少使用,技术上也不需要。

回答by Dharmendra Barad

<?php

class A
{
    private $p;
    private $q;
    private $ans;
    public  function hello()
    {
        echo "Hello PratikPatel";
    }
    public function getdata($a,$b)
    {

        $this->p=$a;
        $this->q=$b;
        $ans=$this ->p/$this->q;
        $this->ans=$ans;

    }
    public function div1()
    {
        echo "your division answer is:=".$this->ans."<br/>";
    }
}
class B extends A
{
    function hello()
    {
        echo "Hello "."<br/>";
    }

}

$p=new B();
$p1=new B();
$p2=new B();
$p -> getdata(20,10);
echo $p1 -> hello();
$p2 -> div1();

?>

?>

this code in problem that can not a print a division result

此代码存在无法打印除法结果的问题