用php来写一个表格

1.<?php
header('content-type:text/html;charset=utf-8');
$table="<table border='0' cellpadding='0' cellspacing='0' width='80%'>";
for($i=1;$i<=3;$i++){
$table.="<tr>";
for($j=0;$j<2;$j++){
$table.="<td>l</td>";
}
$table.="</tr>";
}
$table.="</table>";
echo $table;

?>


2.通过argument.

<?php

header('content-type:text/html;charset=utf-8');
/**
* 创建一个n行m列的表格
* @param number $n 行
* @param number $m 列
*/
function createTable($n,$m){
$table="<table border='0' cellpadding='0' cellspacing='0' width='80%'>";
for($i=1;$i<=$n;$i++){
$table.="<tr>";
for($j=1;$j<=$m;$j++){
$table.="<td>x</td>";
}
$table.="</tr>";
}
$table.="</table>";
return $table;
}
echo createTable(2,5);

?>

3.可选参数

<?php
header('content-type:text/html;charset=utf-8');
/**
* 可选参数

*/
function createTable1($n=3,$m=5,$bgColor='red',$content='x'){
$table="<table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='{$bgColor}'>";
for($i=1;$i<=$n;$i++){
$table.="<tr>";
for($j=1;$j<=$m;$j++){
$table.="<td>{$content}</td>";
}
$table.="</tr>";
}
$table.="</table>";
return $table;
}
echo createTable1(6,3);
echo '<br>';
echo createTable1(3,5,'pink');

?>

必选参数必须在可选参数之前

猜你喜欢

转载自blog.csdn.net/tree_building/article/details/79346514