PHP Print output Checkerboard implementation method, PHP print output board
In this paper, we describe two implementations of the PHP print and output checkerboard. Share to everyone for your reference. The implementation method is as follows:
Example 1, the code is as follows:
Copy the Code code as follows: <?php
/**
* Alternating 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 ' line number must be an integer ';
}
if ($cols! = (int) ($cols)) {
Return ' column number must be an integer ';
}
$str = "";
$str. = "
"; for ($i =1; $i <= $rows; $i + +) {$str. = "
"; for ($j =1; $j <= $cols; $j + +) {if (($i + $j)%2) {$str. = "
"; }else{ $str. = " |
| "; }} $str. = "
"; } $str. = "
";
return $str;
}
Echo fun_table ();
?>
Example 2 simple implementation of the Board-for Loop
To implement the chessboard first we think about what the chessboard is, there are a lot of squares, and then the black and white squares, first we draw the squares, the code is as follows:
Copy the Code code as follows: <?php
echo "
"; for ($i =1; $i <=10; $i + +) {echo "
"; for ($j =1; $j <=10; $j + +) {echo "
54im | "; } echo "
"; } echo "
";
?>
See the above board, consider the black and white emission position, there is a rule can be found that the horizontal and vertical white lattice is the base, black are even, we can use the method to determine the balance of the lattice to show what color, cardinal cell I let him show white, even cell display black, base + even = even, So the even cell (black) We are very good to find out, the remaining is the Cardinal lattice (white), the code is as follows:
Copy CodeThe code is as follows: <?php
/**
Implement the board with a for loop and HTML
**/
echo "
"; for ($i =1; $i <=10; $i + +) {echo "
"; for ($j =1; $j <=10; $j + +) {if (($i + $j)%2) {echo "
"; }else{ echo " |
| "; }} echo "
"; } echo "
";
?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/932072.html www.bkjia.com true http://www.bkjia.com/PHPjc/932072.html techarticle PHP printout of the implementation of the Board, the PHP printout of the board This example describes the PHP printout of the two ways to implement the checkerboard. Share to everyone for your reference. The concrete implementation method is as follows ...