Chess - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Chess.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Exercises: Functions</title>
  5. <style>
  6. table {border: 1px solid black;}
  7. .white {background-color: white; width: 30px; height: 30px;}
  8. .black {background-color: black; width: 30px; height: 30px;}
  9. </style>
  10. </head>
  11. <body>
  12. <?php
  13. /**************
  14. *  Exercise 4  *
  15. **************/
  16. echo "<h3>Exercise 4</h3>";
  17.  
  18. // write your code here
  19. // un-comment the function calls once you have implemented the functions
  20. function odd($number){
  21.     return $number & 1;
  22. } 
  23. function chessboard(){
  24.     echo "<table>";
  25.     for($row = 0; $row < 8; $row++) {
  26.         echo "<tr>";
  27.         for($col = 0; $col < 8; $col++) {
  28.         // even row + even column = white; odd row + odd column = white; everything else = black (if - elseif - else)
  29.             $class = odd($row + $col) ? "white" : "black";
  30.             echo "<td class=\"$class\"></td>";
  31.         }
  32.         echo "</tr>";
  33.     }
  34.     echo "</table>";
  35. } 
  36. chessboard();
  37.  
File Description
  • Chess
  • PHP Code
  • 29 Dec-2022
  • 929 Bytes
You can Share it: