创建3行2列的表格
<?php
/**
* 创建3行2列的表格
* @return string
*/
function createTable()
{
$table = "<table border='1' cellpadding='0' cellspacing='0' width='80%'>";
for ($i = 1; $i <= 3; $i++) {
$table .= "<tr>";
for ($j = 1; $j <= 2; $j++) {
$table .= "<td>x</td>";
}
$table .= "<tr/>";
}
$table .= "</table/>";
return $table;
}
echo createTable();
?>
<?php
/**
* @param int $rows
* @param int $cols
* @param string $bgColor
* @param string $content
* @return string
*/
function createTable($rows = 3, $cols = 5, $bgColor = 'red', $content = "x")
{
$table = "<table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='{$bgColor}'>";
for ($i = 1; $i <= $rows; $i++) {
$table .= "<tr>";
for ($j = 1; $j <= $cols; $j++) {
$table .= "<td>{$content}</td>";
}
$table .= "<tr/>";
}
$table .= "</table/>";
return $table;
}
echo createTable();
?>
必选参数一定在可选参数之前
<?php
/**
* @param int $rows
* @param int $cols
* @param string $bgColor
* @param string $content
* @return string
*/
function createTable($rows, $cols, $bgColor = 'red', $content = "x")
{
$table = "<table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='{$bgColor}'>";
for ($i = 1; $i <= $rows; $i++) {
$table .= "<tr>";
for ($j = 1; $j <= $cols; $j++) {
$table .= "<td>{$content}</td>";
}
$table .= "<tr/>";
}
$table .= "</table/>";
return $table;
}
echo createTable(2, 3);
?>