PHP Print output Checkerboard implementation method, PHP print output checkerboard _php tutorial

Source: Internet
Author: User
Tags php print

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. = " "; }} $str. = " "; } $str. = "
";
}else{
$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 " "; } echo " "; } echo "
54im
";
?>
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 " "; }} echo " "; } echo "
";
}else{
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 ...

  • 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.