Blog Detail

Covid-19 Global Tracker
preview image Programming
by Rocky Paul, Mar 9, 2020, 8:45:41 AM | 2 minutes |

Create a Chess board in PHP

In this blog, I am going to show you how to create a chessboard in PHP. In a chessboard, we have 8 rows and 8 columns with black and white boxes. The history of chess is around 1500 years old. So as we have to draw  64 boxes (8 rows and 8 columns) let us plan how we should proceed.

  • We should run two loops to create the 64 boxes.
  • The outer loop should run 8 times.
  • The inner loop should also run 8 times. So for each outer loop iteration, the inner loop will run 8 times.

Now comes the question, how to plot a table using this loop. So the answer is to create the '<table>' tag before the loop. Put the '<tr>' tag outside the inner loop and put the '<td>' tag inside the inner loop as this will actually draw the boxes.

We should also put the table border so that we can actually see the boxes on the web page. After this, we would be able to generate 64 boxes. Now we should add the condition to change the background colors of the alternate boxes to black and white. This can be achieved by adding the counter of the inner and outer loop and take the modules to apply the background logic.

The last thing is to set the height and width of the boxes so that they can look like the below screenshot:



The above screenshot is showing the output of the PHP program. Please refer to the below PHP program where we have put the complete logic which was used to generate the above chessboard.


<table border = '2'>
<?php
for($i=1; $i<9;$i++)
{
    ?> <tr> <?php
    for($j=1; $j<9; $j++)
    {
        $total = $i+$j;
        if($total%2 == 0)
        {
            ?> <td style="background-color: black;" width = 80px; height = 80px;> </td><?php
        }
        else
        {
            ?> <td width = 80px; height = 80px;> </td><?php
        }
        
    }
    ?> </tr> <?php
}
?>
</table>

So this way we can create a chessboard in PHP. If you have any query then please leave your comments.

About Author

Rocky Paul

View Profile

Comments (0)

Leave a comment

Related Blogs

Create a Pie Chart in Kibana

Dec 24, 2018, 5:25:28 PM | Anurag Srivastava

Introduction to Elasticsearch Aggregations

Aug 14, 2018, 4:47:56 PM | Anurag Srivastava

Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

Bucket Aggregation in Elasticsearch

Aug 29, 2018, 7:15:06 PM | Anurag Srivastava

Elasticsearch Rest API

Jul 31, 2018, 6:16:42 PM | Anurag Srivastava

Basics of Data Search in Elasticsearch

Aug 4, 2018, 7:02:21 AM | Anurag Srivastava

Metrics Aggregation in Elasticsearch

Aug 18, 2018, 6:02:20 PM | Anurag Srivastava

Handling files and folders using Python

Feb 2, 2018, 7:29:35 PM | jitender yadav

Handle Excel file using Python (Part 1)

Dec 30, 2019, 11:20:08 AM | Anurag Srivastava

Handle Excel file using Python (Part 2)

Dec 31, 2019, 1:33:53 PM | Anurag Srivastava

Top Blogs

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Deploying Angular code using Python script

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

Configuring Logstash to send MongoDB data into Elasticsearch

Mar 9, 2019, 8:20:38 AM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Elasticsearch Rest API

Jul 31, 2018, 6:16:42 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava