How to print chess board in php ?
January 5, 2018
In this article, we are going to learn to code the Chess board in php. I will present you a code to get the output of Chess board. We will take two loops, one loop for rows and other inner loop for columns.
In the loop we will take ‘if’ condition with modulus (%) operator, The modulus (%) operator is to check if an integer is odd or even. If integer is even then white background else black background. In this way we can get the out put of Chess board.
<?php <table width="310px" cellspacing="0px" cellpadding="0px" border="1px"> <?php for($row=1;$row<=8;$row++) { echo "<tr>"; for($col=1;$col<=8;$col++) { $total=$row+$col; if($total%2==0) { echo "<td height='35px' width='35px' bgcolor='#FFFFFF'></td>"; } else { echo "<td height='35px' width='35px' bgcolor='#000000'></td>"; } } echo "</tr>"; } ?> </table>