使用PHP在HTML表格中显示字母表

use*_*238 0 php html-table

我会尽量简短.

我如何使用PHP将所有26个字母表显示在一个表格中(6行4列,最后一行2列)?

每个表块都应包含字母表中的字母,例如:

A B C D

EFGH

IJKL

MNOP

QRST

UVWX

YZ

我应该用PHP编写函数,我假设将它回显到HTML表中.

任何帮助将不胜感激!

and*_*eas 8

这可能是一种实现

// All letters of the alphabet
$alphabet = range('A', 'Z'); // range returns an array

$table = '<table>';

for ($i = 0; $i < count($alphabet); $i++) {

    // Every fourth element start a new table line
    if ($i % 4 == 0)
        $table .= '<tr>';

    $table .= '<td>' . $alphabet[$i] . '</td>';

    // Every fourth element end a table line. Do not forget the last element
    if (($i-3) % 4 == 0 || $i+1 == count($alphabet))
        $table .= '</tr>';
}

$table .= '</table>';

// Do whatever you want with the output string
echo $table;
Run Code Online (Sandbox Code Playgroud)

  • 为什么要使用`$ alphabet ='ABCDEFGHIJKLMNOPQRSTUVWXYZ';`?为什么不`$ alphabet = range('A','Z');` (2认同)