Home > Backend Development > PHP Tutorial > Implement an interesting division table using PHP

Implement an interesting division table using PHP

藏色散人
Release: 2023-04-10 14:52:02
Original
3952 people have browsed it

In the article "PHP Fun Output 6*6 Multiplication Table", I introduced you how to implement a multiplication table, so this article will introduce to you how to implement a division table through PHP.

The division table introduced in this article is different from the familiar multiplication table (starting from "one one gets one" and ending with "nine-nine eighty-one"). The division table implemented below is just Let everyone understand the idea of ​​​​learning to use PHP to implement the 10X10 division table, instead of asking everyone to recite the multiplication table, haha~

Not much to say, I wonder if you have any ideas about this, you can try it locally Take a look~

The following is an implementation idea and code I gave:

The complete code for PHP to implement the division table is as follows:

<?php
$start  = 1;
$end = 10;
?>
<html>
<head>
    <title></title>
</head>
<body>
<table border="1">
    <?php
    print("<tr>");
    print("<th></th>");
    for($count = $start; $count <= $end; $count++)
        print("<th>".$count."</th>");
    print("</tr>");

    for($count = $start; $count <= $end; $count++)
    {
        print("<tr><th>".$count."</th>");
        for($count2 = $start; $count2 <= $end; $count2++)
        {
            $result = $count / $count2;
            printf("<td>%.3f</td>", $result);
        }
        print("</tr> \n");
    }
    ?>
</table>
</body>
</html>
Copy after login

The output result is as follows:

Implement an interesting division table using PHP

Isn’t it interesting?

So in this code, the body of the code has a for loop nested within another loop, each loop is executed ten times and produces a 10 X 10 table. In the loop, each iteration of the outer loop prints one row, while each iteration of the inner loop prints one cell.

About the for loop, here is a brief introduction:

In PHP, if you have determined the number of times the script will run in advance, you can use the for loop.

For loop syntax

for (init counter; test counter; increment counter) {
  code to be executed;
}
Copy after login

The parameters respectively represent:

init counter: initialize the value of the loop counter;

test counter:: Evaluated for each loop iteration. If the value is TRUE, continue looping. If its value is FALSE, the loop ends;

increment counter: Increase the value of the loop counter.

→P.S. The foreach loop only works on arrays and is used to iterate through each key/value pair in the array.

Finally, I would like to recommend to you the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of Implement an interesting division table using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template