批改状态:未批改
老师批语:
今天的课程学到了函数:
函数:有参数和无参数函数。
无参数:function test(){ return 'sss' }
有参数函数:
必须传参;默认值传参
必须:function test ( $arr ) { return $arr; } 不传入参数报错。
默认值: function test ( $arr='空' ){ return $arr; }
函数可以多个参数 ;function test( $1, $2 ,$3... ){}
作业:循环拼装表格
<?php
/**
* Created by PhpStorm.
* User: hello word!
* Date: 2019/3/19
* Time: 22:15
*/
header('content-type:text/html;charset=utf-8');
//漫画信息
$animate = [
['id'=>1, 'name'=>'火影', 'country'=>'jp','hot'=>0],
['id'=>2, 'name'=>'海贼', 'country'=>'jp','hot'=>1],
['id'=>3, 'name'=>'我的英雄学院', 'country'=>'jp','hot'=>0],
['id'=>4, 'name'=>'死神', 'country'=>'jp','hot'=>1],
['id'=>5, 'name'=>'全职猎人', 'country'=>'jp','hot'=>0],
];
$title='我喜欢的漫画';
$tableTitle="漫画列表";
function createTable($animate)
{
$result = '';
foreach ($animate as $v)
{
$result .= '<tr>';
$result .= '<td>' . $v['id'] . '</td>';
$result .= '<td>' . $v['name'] . '</td>';
$result .= '<td>' . $v['country'] . '</td>';
$result .= '<td>' . ($v['hot'] ? '热门' : '不热门') . '</td>';
$result .= '</tr>';
}
return $result;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $title; ?></title>
<style>
table,th,td {
border: 1px solid #666;
padding: 8px;
}
table {
border-collapse: collapse;
width: 80%;
text-align: center;
margin: 30px auto;
}
thead tr:first-of-type {
background-color: lightblue;
}
table > caption {
font-size: 1.2rem;
margin-bottom: 15px;
}
table + p {
text-align: center;
}
</style>
</head>
<body>
<table>
<caption>
<?php
echo '<span style="color:red">' . $tableTitle . '</span>';
?>
</caption>
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>国家</th>
<th>是否热门</th>
</tr>
</thead>
<tbody>
<?php echo createTable($animate); ?>
</tbody>
</table>
</body>
</html>点击 "运行实例" 按钮查看在线实例
总结:php与html的混编可以完成很多的东西。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号