Mastering PHP: Multiplication Table

I’ve been devoting time for my PHP learning since Saturday. Actually, one prerequisite of mastering PHP is that you should already have a flair for web publishing using HTML markups. If you’re a programmer (using C and Perl), learning PHP is much easier as it almost has similar syntax scheme like on control structures.

From now on, I’ll post all stuff I’ve discovered about PHP (which is a recursive acronym for PHP: Hypertext Preprocessor).

I made use of the recursive-function feature of PHP to produce a multiplication table at 10 by 10. The code I wrote looks like this:

<html><head><title>Multiplication Table</title></head>

<body><?php

$i = 1;

echo "<table border=\"1\">";

echo "<tr>";

WriteRow($i);

echo "</table>";

function WriteRow($i) {

for ($j = $i; $j <= $i * 10; $j += $i) {

echo "<td align=\"center\">";

echo " $j ";

echo "</td>";

}

echo "</tr>";

echo "<br>";

if ($i > 9) { exit; }

echo "<tr>";

WriteRow($i + 1);

}

?>

</body>

</html>
Advertisements

9 comments

  1. It can be easier

    <?php

    for($i=1; $i<=10; $i++){
    print(“\n”);
    for($a=1; $a<=10; $a++){
    $b = $a * $i;
    print(” $b\n”);
    }
    print(“\n”);
    }

    ?>

  2. Multiplication Table

    <?php
    echo "”;
    for($a=1; $a<=20; $a++){
    echo '’;
    for($b=1; $b<=20; $b++){
    echo"”.($a * $b);echo””;
    }
    echo ”;
    }
    echo “”;
    ?>

  3. @isla:
    the 2nd to the last echo was missing its pair of quotation mark.. 🙂

    @dungkal:
    you’re blog site is helpful for my homework, though we are told to input numbers and output a multiplication table using the limit of that number. For example, I input 12, then the output should be a table 12×12.. Its a good start though, reading you’re site.. 🙂

  4. Hi you can also try this piece of code…. Really simple

    <?php
    for ($i = 1; $i <= 10; $i++) {
    echo "”;
    $counter = $i;
    for ($k = 1 ; $k<=10;$k++){

    $j = $counter * $k;
    echo " $j”;
    }
    echo ” “;
    }
    ?>

  5. <?php
    //Ghufran Javed From Pakistan Peshawar +923419171941
    echo "

    No
    Operator
    multiplication
    Equal
    result
    “;
    $a=5;
    $b;
    $c;
    for ($b=1;$b<=10;$b++)
    {
    $c=$a*$b;
    echo "
    $a
    *
    $b
    =
    $c
    “;
    echo “”;
    }
    ?>

  6. <?php

    $values = range(1,15); // same as $values = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
    foreach($values as $side){ // Creating each row//foreach essentially say "do this to everything in this set",rather than "do this x times.
    echo '’;
    foreach ($values as $top){ // Creating each column in each row
    $output = $side * $top; // multiple the top value times the side value
    echo “$output”;
    }
    echo ”;
    }
    ?>

  7. i am going to write a multiplication program that takes a number from user and print its table this program is divided in two parts html form (to get number from user) and php script(here we write our multiplication code)…………………..
    Part-1

    Enter Number:

    Part – 2

    <?php
    $a=$_POST["a"];
    for($i=1; $i<=10; $i++){
    $c=$a*$i;
    echo "$a x $i = $c “;
    }

    ?>

  8. Multiplication Table

    <?php

    $i = 1;

    echo "”;

    echo “”;

    WriteRow($i);

    echo “”;

    function WriteRow($i) {

    for ($j = $i; $j <= $i * 10; $j += $i) {

    echo "”;

    echo ” $j “;

    echo “”;

    }

    echo “”;

    echo “”;

    if ($i > 9) { exit; }

    echo “”;

    WriteRow($i + 1);

    }

    ?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s