PHP/Language Basics/Static

Материал из Web эксперт
Перейти к: навигация, поиск

Use static variable defined in function

 <?php
  increment ();
  increment ();
  increment ();
  function increment (){
      static $a=0;
      echo $a;
      $a++;
  }    
  ?>



Using the static Statement to Remember the Value of a Variable Between Function Calls

<html>
<head>
<title>Using the static Statement to Remember the Value of a Variable Between Function Calls</title>
</head>
<body>
<?php
function aFunction( $txt ) {
     static $numCalls = 0;
     $numCalls++;
     print "<h1>$numCalls. $txt</h1>";
}
aFunction("A");
print("static called<p>");
aFunction("B");
print("called again<p>");
?>
 </body>
</html>