Use static variable in PHP

Description

The following code shows how to use static variable.

Example


<?/*from w w  w .j a v  a2 s.  c  o  m*/
  function useColor()
  {
    //remember the last color we used
    static $ColorValue = "#00FF00";

    //choose the next color
    if($ColorValue == "#00FF00")
    {
      $ColorValue = "#CCFFCC";
    }
    else
    {
      $ColorValue = "#00FF00";
    }

    return($ColorValue);
  }

  print("<table width=\"300\">\n");
  for($count=0; $count < 10; $count++)
  {
    //get color for this row
    $RowColor = useColor();

    /*
    ** print out HTML for row
    ** set background color
    */
    print("<tr>" .
    "<td style=\"background: $RowColor\">" .
    "Row number $count" .
    "</td>" .
    "</tr>\n");
  }
  print("</table>\n");
?>




















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition