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

关于static变量,该如何解决

WBOY
Release: 2016-06-13 10:47:58
Original
898 people have browsed it

关于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++;
}

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template