[PHP函数]②--函数参数

创建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();

?>
Paste_Image.png
Paste_Image.png
Paste_Image.png
<?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();

?>
Paste_Image.png

必选参数一定在可选参数之前

<?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);


?>
Paste_Image.png
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,156评论 6赞 13
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 6,492评论 0赞 4
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 13,717评论 2赞 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,251评论 19赞 139
  • 经常有女生问, 真的没有那种很喜欢你却羞于表达的男生吗? 我也不知道答案是什么。 记得初中班里有一个男生, 他木讷...
    白说也要说阅读 154评论 0赞 0

友情链接更多精彩内容