首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 實作 99 乘法表:初學者練習範例與多種輸出格式

     編輯:PHP 相關知識

99 乘法表是經典的程式設計入門練習,本文提供多種 PHP 實作方式,從基本巢狀迴圈到格式化輸出,適合初學者理解迴圈邏輯與字串處理。

基本版:簡單文字輸出

<?php
for ($a = 1; $a <= 9; $a++) {
    for ($b = 1; $b <= 9; $b++) {
        echo "$a*$b=" . ($a * $b) . "\t";
    }
    echo "\n";  // 換行
}
?>

HTML 表格版:適合網頁顯示

<?php
echo "<table border='1' cellpadding='5' style='border-collapse:collapse;'>\n";

for ($i = 1; $i <= 9; $i++) {
    echo "  <tr>\n";
    for ($j = 1; $j <= 9; $j++) {
        $product = $i * $j;
        // 對角線以上顯示,以下留白(可選)
        if ($j >= $i) {
            echo "    <td>$i × $j = $product</td>\n";
        } else {
            echo "    <td>&nbsp;</td>\n";
        }
    }
    echo "  </tr>\n";
}

echo "</table>\n";
?>

完整 HTML 頁面範例

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>99 乘法表</title>
    <style>
        table { border-collapse: collapse; margin: 20px auto; font-family: monospace; }
        td { border: 1px solid #ddd; padding: 8px 12px; text-align: center; min-width: 60px; }
        tr:first-child td { background: #f0f0f0; font-weight: bold; }
        td:first-child { background: #f0f0f0; font-weight: bold; }
        .highlight { background: #fff3cd; }
    </style>
</head>
<body>
    <h1 style="text-align:center;">99 乘法表</h1>
    
    <?php
    echo "<table>\n";
    echo "  <tr><th>×</th>";
    for ($col = 1; $col <= 9; $col++) echo "<th>$col</th>";
    echo "</tr>\n";
    
    for ($row = 1; $row <= 9; $row++) {
        echo "  <tr><th>$row</th>";
        for ($col = 1; $col <= 9; $col++) {
            $class = ($col >= $row) ? ' class="highlight"' : '';
            echo "<td$class>";
            if ($col >= $row) echo "$row × $col = " . ($row * $col);
            echo "</td>";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
    ?>
</body>
</html>

函數封裝版:可複用元件

<?php
/**
 * 產生乘法表陣列
 * @param int $max 最大乘數 (預設 9)
 * @param bool $triangular 是否只顯示上三角 (預設 true)
 * @return array 二維陣列 [row][col] = '表達式 = 結果'
 */
function generateMultiplicationTable($max = 9, $triangular = true) {
    $table = [];
    for ($i = 1; $i <= $max; $i++) {
        $table[$i] = [];
        for ($j = 1; $j <= $max; $j++) {
            if (!$triangular || $j >= $i) {
                $table[$i][$j] = "$i × $j = " . ($i * $j);
            } else {
                $table[$i][$j] = '';
            }
        }
    }
    return $table;
}

/**
 * 將乘法表渲染為 HTML 表格
 */
function renderTableHtml(array $table) {
    $max = count($table) - 1;
    $html = "<table>\n";
    $html .= "  <tr><th>×</th>";
    for ($c = 1; $c <= $max; $c++) $html .= "<th>$c</th>";
    $html .= "</tr>\n";
    
    for ($r = 1; $r <= $max; $r++) {
        $html .= "  <tr><th>$r</th>";
        for ($c = 1; $c <= $max; $c++) {
            $val = $table[$r][$c];
            $html .= "<td>" . ($val ? htmlspecialchars($val) : '&nbsp;') . "</td>";
        }
        $html .= "</tr>\n";
    }
    $html .= "</table>\n";
    return $html;
}

// 使用範例
$table = generateMultiplicationTable(12, false);  // 12x12 完整表
echo renderTableHtml($table);
?>

進階變體:不同進制輸出

<?php
// 十六進制乘法表
for ($i = 1; $i <= 15; $i++) {
    for ($j = 1; $j <= 15; $j++) {
        printf("%2s × %2s = %3s  ", dechex($i), dechex($j), strtoupper(dechex($i * $j)));
    }
    echo "\n";
}
?>

學習重點總結

  • 巢狀迴圈:外層控制行(被乘數),內層控制列(乘數)
  • 字串串接:. 運算子與雙引號變數解析
  • 格式化輸出:printf()、sprintf()、str_pad() 對齊欄位
  • 條件判斷:控制三角形/完整矩形顯示
  • 函數封裝:將邏輯與呈現分離,提高複用性

練習建議:嘗試修改程式碼產生「加法表」、「除法表」或「任意 N×N 乘法表」,加深對迴圈邊界條件的理解。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved