How to implement PHP printed and exported chessboard _php tips

Source: Internet
Author: User

This article illustrates two ways to implement the printed and exported chessboard of PHP. Share to everyone for your reference. The implementation methods are as follows:

Example 1, the code is as follows:

Copy Code code as follows:
<?php
/**
* Alternate color of interlaced columns
* String fun_table (int $rows =9,int $cols =9)
* $rows indicates that the number of rows must be an integer and must be between 1-20
* $cols indicates that the number of columns must be an integer and must be between 1-20
*/
function fun_table ($rows =9, $cols =9) {
if ($rows <1 | | $rows >20) {
Return "must be an integer and must be between 1-20";
}
if ($cols <1 | | $cols >20) {
Return "must be an integer and must be between 1-20";
}
if ($rows!= (int) ($rows)) {
Return ' number of rows must be an integer ';
}

if ($cols!= (int) ($cols)) {
Return ' number of columns must be an integer ';
}
$str = "";
$str. = "<table cellspacing= ' 0 ' width= ' 500px ' border = ' 1px ' bordercolor= ' black ' >";
for ($i =1; $i <= $rows; $i + +) {
$str. = "<tr>";
for ($j =1; $j <= $cols; $j + +) {
if (($i + $j)%2) {
$str. = "<td height= ' 50px ' bgcolor= ' black ' >";
}else{
$str. = "<td></td>";
}
}
$str. = "</tr>";
}
$str. = "</table>";
return $str;
}
Echo fun_table ();
?>

Example 2 simple implementation of the chessboard-for cycle

To achieve this chessboard first we think about how the chessboard is, there are many square composition, and then by the black and white of the square, first we draw the square, the code is as follows:

Copy Code code as follows:
<?php
echo "<table cellspacing= ' 0 ' width= ' 500px ' border = ' 1px ' bordercolor= ' black ' >";
For ($i =1 $i <=10; $i + +) {
echo "<tr>";
For ($j =1 $j <=10; $j + +) {
echo "<td>54im</td>";
}
echo "</tr>";
}
echo "</table>";
?>

When you see the above chessboard, consider the position of the black and white emissions, there is a rule that can be found, horizontal and vertical row of white lattice is cardinal, black is even, we can use the method to determine what color the lattice, Cardinal cell I let him show white, even cells show black, cardinality + even = even, So even cell (black) We're good to find out, the rest is the cardinality (white), the code is as follows:
Copy Code code as follows:
<?php
/**
Implementing a chessboard with a for loop and HTML
**/
echo "<table cellspacing= ' 0 ' width= ' 500px ' border = ' 1px ' bordercolor= ' black ' >";
For ($i =1 $i <=10; $i + +) {
echo "<tr>";
For ($j =1 $j <=10; $j + +) {
if (($i + $j)%2) {
echo "<td height= ' 50px ' bgcolor= ' black ' >";
}else{
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</table>";
?>

I hope this article will help you with your PHP program design.

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.