Php printing and output Board Implementation Method

Source: Internet
Author: User
This article mainly introduces the implementation of the php printing and output board, and analyzes two different implementation methods in the form of examples, which has some reference value. If you need them, you can refer

This article mainly introduces the implementation of the php printing and output board, and analyzes two different implementation methods in the form of examples, which has some reference value. If you need them, you can refer

This article describes two ways to implement the php printing and output board. Share it with you for your reference. The specific implementation method is as follows:

Example 1: the code is as follows:

The Code is as follows:

<? Php
/**
* Change the color of every row and column
* 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 and 20
* $ Cols indicates that the number of columns must be an integer and must be between 1 and 20.
*/
Function fun_table ($ rows = 9, $ cols = 9 ){
If ($ rows <1 | $ rows> 20 ){
Return "must be an integer and must be between 1 and 20 ";
}
If ($ cols <1 | $ cols> 20 ){
Return "must be an integer and must be between 1 and 20 ";
}
If ($ rows! = (Int) ($ rows )){
Return 'number of rows 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 this board, first let's think about the board. There are a lot of squares, and then it is composed of black and white squares. First, let's draw the square. The Code is as follows:

The Code is as follows:

<? Php
Echo"









";For ($ I = 1; $ I <= 10; $ I ++ ){Echo" ";For ($ j = 1; $ j <= 10; $ j ++ ){Echo" ";}Echo" ";}Echo"
54im
";
?>


After seeing the board above, consider the location of the black and white grids. There is a rule that we can find that the white grids on the horizontal and vertical bars are the base numbers and the black ones are even numbers, we can use the remainder method to determine the color of the cell to be displayed. The base cell is white and the even cell is black. The base + even number is even, so we can find the even number of cells (black), and the rest is the base cell (white). The Code is as follows:

The Code is as follows:

<? Php
/**
Implement the Board through the 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 will help you with php programming.

,

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.