Using the static modifier variable inside a function in PHP

Source: Internet
Author: User
Tags getcolor

First understand the difference between static variables, declare a function test ()

function num () {    $a = 0;     Echo $a ;     $a+ +;} num (); num (); Num (); // Output
function num () {    static$a = 0;     Echo $a ;     $a+ +;} num (); num (); Num (); // Output 012

From the above you can see the difference between the use of static, static can be repeated when the function is called, the static variable retains the result of the last execution of the function.

Such properties can be used to make interlaced discoloration, etc.

functionGetColor () {Static $colorvalue; if($colorvalue= = ' #FFF ')        $colorvalue= ' #000 '; Else        $colorvalue= ' #FFF '; return $colorvalue;}Echo"<table border=1>"; for($i= 0;$i<10;$i++) {    $bgcolor=GetColor (); Echo"<tr bgcolor=$bgcolor> "; Echo"<td>$i</td> "; Echo"</tr>";}

Http://www.crackedzone.com/php-static-variable-summary.html

Using the static modifier variable inside a function in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.