Printing Colorful Table of a Number in PHP

0
251
Table of a Number in PHP

Hey, Guys What up? Here’s another article in our PHP Programming Tutorials Series. You can follow along with the PHP programs for Beginners here, today we will learn how you can print a colorful table of a number in PHP. If you want to contribute to programmingy, you can submit your articles here. If you want to understand some concepts or want a program for some topic, please request code here.

HTML File to print Table of a Number in PHP

We set the $n to 10 You can make it any number or You can also take User Input. You can learn Taking user input in PHP here.  We simply used some HTML tags using Echo Statements, If you’re not sure how to use HTML with PHP. Please learn the Basic HTML concept in PHP here. We used a ‘for loop’ – from 1 to 10. We simply used an If-else condition, so that it uses different colors foralternate rows. Inside if and else blocks, we used <tr> and <td> tags to create tables rows and columns {learn how to create tables in HTML and PHP}.

<?php
echo "<table>"; //We are opening our table
$n=10; 
for($i=1;$i<=10;$i++){
if($i%2==0){ //Condition for Even Rows
    echo "<tr><td style=background-color:orange>$n * $i = ".($n*$i)."</tr></td>";
}
else //Obviously for odd rows
{
    echo "<tr><td style=background-color:lightblue>$n * $i = ".($n*$i)."</tr></td>";
}

}
echo "</table>"; //We are closing our table here.

?>

USING FOR LOOP IN PHP:

It’s very simple – write ‘for’ keyword. In parenthesis () – You write 3 things, separated by ; (semicolons). First, initial value – from which you want to initiate loop. Second, till what position you’d like to run your loop. And the final is increment or decrement. $i++ simply means – Increase the value of $i by 1 after each iteration.

Rest of the code is quite easy but still, if you have any doubts in printing Table of a Number in PHP or any example code above, please comment below and let us solve the concept for you. On a side note, guys if you liked this article – please appreciate the time and hard work I am trying to put in here. You can give us at least one share on Facebook or Google+. Hope You will do share. Thanks and Keep Coding.

LEAVE A REPLY

Please enter your comment!
Please enter your name here