Home  >  Article  >  Backend Development  >  Summary of methods to implement the ninety-nine multiplication table of PHP classic basic test questions

Summary of methods to implement the ninety-nine multiplication table of PHP classic basic test questions

伊谢尔伦
伊谢尔伦Original
2017-06-21 13:43:151983browse

For those new to PHP, writing the Nine-Nine Multiplication Table in PHP is undoubtedly a very classic practice question. It is quite a test of logic.

The so-called difficult thing is not difficult for those who know it, but it is not difficult for those who know it. For some veterans, this is really nothing. But for novices, it can train logical thinking.

If there are no restrictions, you may be able to type the entire code in two minutes. If you are proficient, you can also implement it in several ways. But what if you are asked to write the multiplication table for four angles? (It can continue to be extended)

The following will introduce several methods to implement the nine-nine multiplication table of PHP classic basic test questions:

1. Use for loop to print nine Multiplication table:

<?php
for($j=1; $j<=9; $j++) {
for($i=1; $i<=$j; $i++) {
echo "{$i}x{$j}=".($i*$j)." ";
}
echo "<br />";
}

2. Use while loop to print the multiplication table

<?php
$j = 1;
while($j<=9){
$i = 1;
while($i<=$j){
echo "{$i}x{$j}=".($i*$j)." ";
$i++;
}
echo "<br />";
$j++;
}

3. Use do while loop to print the multiplication table

<?php
$j = 1;
do {
$i = 1;
do {
echo "{$i}x{$j}=".($i*$j)." ";
$i++;
} while($i<=$j);
echo "<br />";
$j++;
} while($j<=9);

Use a for loop to output the ninety-nine multiplication table in the form of table

Angle one: (the most common conventional writing method)

<?php
echo "<table width=&#39;600&#39; border=&#39;1&#39;>";
for($j=1;$j<=9;$j++){
echo "<tr>";
for($i=1;$i<=$j;$i++){
echo "<td>{$i}*{$j}=".($i*$j)."</td>";
}
echo "</tr>";
}
echo "</table>";

Angle two: (common to the conventional writing method X-axis symmetry)

<?php
echo "<table width=&#39;600&#39; border=&#39;1&#39;>";
for($j=9;$j>=1;$j--){
echo "<tr>";
for($i=1;$i<=$j;$i++){
echo "<td>{$i}*{$j}=".($i*$j)."</td>";
}
echo "</tr>";
}
echo "</table>";

Angle three: (Y-axis symmetry with angle two)

<?php
echo "<table width=&#39;600&#39; border=&#39;1&#39;>";
for($j=9;$j>=1;$j--){
echo "<tr>";
for($z=0;$z<9-$j;$z++){
echo "<td> </td>";
}
for($i=1;$i<=$j;$i++){
echo "<td>{$i}*{$j}=".($i*$j)."</td>";
} 
echo "</tr>";
}
echo "</table>";

Angle four: (Y-axis symmetry with conventional writing)

<?php
echo "<table width=&#39;600&#39; border=&#39;1&#39;>";
for($j=1;$j<=9;$j++){
echo "<tr>";
for($z=0;$z<9-$j;$z++){
echo "<td> </td>";
}
for($i=$j;$i>=1;$i--){
echo "<td>{$i}*{$j}=".($i*$j)."</td>";
} 
echo "</tr>";
}
echo "</table>";

The above is the detailed content of Summary of methods to implement the ninety-nine multiplication table of PHP classic basic test questions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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