Coding Chessboard using for loop without Modulus in PHP

0
PHP chessboard for loop without modulus

ChessBoard with For Loop without Modulus in PHP

As we already wrote a tutorial on how to make chessboard layers using PHP for loop. Here in this tutorial we are going to make chessboard but without use of Modulus in it. So lets start our tutorial and see what kind of code will come out.

Here is our PHP code

<?php

$a = 8;
$tdcount = 0;
$trcount = 0;
echo '<table>';

for($k=1; $k <= $a; $k++)
{
    $trcount++;
    echo '<tr>';
    
    for($j=1; $j<= $a; $j++)
    {
        $tdcount++;
        
        if(is_int($trcount/2))
        {
            if(is_int($tdcount/2))
            {
                echo '<td></td>';
            }
            else
            {
                echo '<td class="black"></td>';
            }
        }
        else
        {
            if(is_int($tdcount/2))
            {
                echo '<td class="black"></td>';
            }
            else
            {
                echo '<td></td>';
            }			
        }
    }
    echo '</tr>';
}

echo '</table>';


?>

 

in this above code instead of using Modulus to find even and odd boxes to color black in it , we used is_int() function which is inbuilt function of PHP. So out of 64 boxes which ever number divided by 2 gives fraction means its float for example. 3/2 = 1.5 means its float and is_int() is checking only integers which means number must be absolute without fraction.

I hope this will help our reader how to design a chessboard without using modulus.

Do you like more PHP tutorials ?

 

Live Demo for the above codes : https://websitecoder.in/guideblog/chessboard/

Leave a Reply

Your email address will not be published. Required fields are marked *

  +  68  =  77