博客列表 >php基础——五期线上培训——2019-3-19

php基础——五期线上培训——2019-3-19

黄健的博客
黄健的博客原创
2019年03月19日 22:41:00728浏览

今天的课程学到了函数:

    函数:有参数和无参数函数。

        无参数: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的混编可以完成很多的东西。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议