Home  >  Article  >  Backend Development  >  关于static变量,该如何解决

关于static变量,该如何解决

WBOY
WBOYOriginal
2016-06-13 10:47:58885browse

关于static变量
  function A(){
  static $a=1;
  echo $a++;
  }
  A(); // 1
  A(); // 2
  A(); // 3
?>
但这么写就会出现问题:
  function A(){
  static $a;
$a=1;
  echo $a++;
  }
  A(); // 1
  A(); // 1
  A(); // 1
?>

这是为何?

------解决方案--------------------
很简单,static 第二次声明是忽略的
你试试

function A(){
static $a=1;
$a=1;
echo $a++;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn