Using static to realize the color interlaced display of the table

Source: Internet
Author: User
Tags getcolor variable
Display the use of static to achieve the color of the table interlaced display
In practical programming applications, we often forget about static functions. Have a good reflection. :)

We use PHP to query the data from the database, and output the results to the browser, if the results are many rows, the table bgcolor if all are monochrome, the viewer will not feel very comfortable. So how do you make the rows in the table different colors?
Please look below:
?
function GetColor ()
{
static $colorvalue;//define a statically variable
if ($colorvalue = = "#ffffff")
$colorvalue = "#000000";
else $colorvalue = "#ffffff";
return ($colorvalue);
}

Print ("<table border=1>n");//output 10 lines below
For ($i =0 $i <10; $i + +)
{$bcolor =getcolor ()//Change background color
Print ("<tr bgcolor= $bcolor >n");
Print ("<td> $i </td>n");
Print ("</tr>");
}
Print ("</table>n");
Description
A static variable is defined in this program $colorvalue meaning that after the function call ends, the
This variable $colorvalue also retains the value and does not disappear. When the GetColor () function is called again, the value of the variable $colorvalue is the $colorvalue value at the end of the last function call.


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.