****************************** 第五章 代码重用与函数编写 *********************************

代码重用的好处;使用require()和include()函数;函数介绍;定义函数;使用参数;理解作用域;

返回值;参数的引用调用和值调用;实现递归;使用命名空间

*************** 5.1 代码重用的好处

1.成本低;2.可靠性;3.一致性:系统的外部接口是一致的,其中包括用户接口和系统的外部接口。

*************** 5.2 使用require()和include()函数

使用一条require()或include()语句,可以将一个文件载入到PHP脚本中,这个文件可以包含任何希望在一个脚本中输入的内容,

其中包括PHP语句、文本、HTML标记、PHP函数或PHP类。

(同C语言的#include一样)

两者区别:函数失败后,require()函数将给出一个致命错误,而include()只是给一个警告。

变体函数:require_once()和include_once(),确保包含的文件只能被引入一次。通常用于页眉和脚注(header and footer)。

*************** 5.2.1 文件扩展名和require()函数

现在有一个reusable.php文件:

<?php
echo "Here is a very simple PHP statement.<br />";
?>

还有一个main.php文件:

<?php
echo "This is the main file.<br />";
require('reusable.php');
echo "The script will end now.<br />";
?>

我们注意到在main.php文件中使用了require()函数引用了reusable.php文件,那么打印结果为:

This is the main file.
Here is a very simple PHP statement.
The script will end now.

注意:当使用require()语句时,必须注意处理文件扩展名和PHP标记的不同方式。

可以使用任意扩展名来命名包含文件,但要尽量遵循一个规则,将扩展名命名为.inc或.php。
 
 .inc文件(include file):实际上,文件的后缀对于文件包含无所谓,一般使用inc为后缀,这样能体现该文件的作用。

*************** 5.2.2 使用require()制作Web站点的模板

如果一个网站有几十上百个网页,而且他们风格一致,只是有一些细微的改变,相对于剪切粘贴数十个、数百个甚至数千个页面,直接重用各个页面中通用的

HTML代码部分是一个更好的办法。

例子:home.html —— TLA咨询公司主页的HTML脚本

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>TLA Consulting Pty Ltd</title><style type="text/css">h1{color:white;font-size: 24pt;text-align: center;font-family: Arial,sans-serif;}.menu{color: white  ;font-size: 12pt;text-align: center;font-family: Arial,sans-serif;font-weight: bold;}td{background: black;}p{color: black;font-size: 12pt;text-align: justify;font-family: Arial,sans-serif;font-weight: bold;}a:link,a:visited,a:active{color: white;}</style>
</head>
<body>
<!-- page header -->
<table width="100%" cellpadding="12" cellspacing="0" border="0"><tr bgcolor="black"><td align="left"><img src="logo.gif" alt="TLA logo" height=70 width=70></td><td><h1>TLA Consulting</h1></td><td align="right"><img src="logo.gif" alt="TLA logo" height=70 width=70></td></tr>
</table><!-- menu -->
<table width="100%" bgcolor="white" cellpadding="4" cellspacing="4"><tr ><td width="25%"><img src="s-logo.gif" alt="" height=20 width=20> <span class="menu">Home</span></td><td width="25%"><img src="s-logo.gif" alt="" height=20 width=20> <span class="menu">Contact</span></td><td width="25%"><img src="s-logo.gif" alt="" height=20 width=20> <span class="menu">Services</span></td><td width="25%"><img src="s-logo.gif" alt="" height=20 width=20> <span class="menu">Site Map</span></td></tr>
</table><!-- page content -->
<p>Welcome to the home of TLA Consulting.Please take some time to get to know us.</p>
<p>We specialize in serving your business needsand hope to hear from you soon.</p><!-- page footer -->
<table width="100%" bgcolor="black" cellpadding="12" border="0"><tr><td><p class="foot">&copy; TLA Consulting Pty Ltd.</p><p class="foot">Please see our <a href="legal.php">legal information page</a></p></td></tr>
</table>
</body>
</html>

我们可以看到这个文件由许多不同的代码部分组成:

HTMl标题包含了在该页面中用到的级联风格样式单(CSS)中的样式定义==>

<head><meta charset="UTF-8"><title>TLA Consulting Pty Ltd</title><style type="text/css">h1{color:white;font-size: 24pt;text-align: center;font-family: Arial,sans-serif;}.menu{color: white  ;font-size: 12pt;text-align: center;font-family: Arial,sans-serif;font-weight: bold;}td{background: black;}p{color: black;font-size: 12pt;text-align: justify;font-family: Arial,sans-serif;font-weight: bold;}a:link,a:visited,a:active{color: white;}</style>
</head>

标有“page header”部分显示了公司的名称(TLA Consulting)和徽标(logo.gif);

标有“menu”部分创建了页面的导航条;

标有“page content”部分是页面中的文本;

然后是脚注。

我们将这个文件分割,然后给这些部分分别命名为header.php, home.php, footer.php。

这样,文件header.php 和 footer.php中都包含有在其他页面中可以重用的代码。

像下面这样:

1.我们用home.php代替home.html,它包含页面内容和两个require语句:

home.php —— TLA公司主页的php脚本

<?php
require('header.inc');
?>
<!-- page content -->
<p>Welcome to the home of TLA Consulting.
Please take some time to get to know us.</p>
<p>We specialize in serving your business needs and hope to hear from you soon.</p>
<?php
require('footer.inc');
?>

2.文件header.inc包含了页面使用的级联风格样式单定义以及公司名称和导航

header.inc —— 所有TLA网站的页面可重复使用的页眉

<html lang="en">
<head><meta charset="UTF-8"><title>TLA Consulting Pty Ltd</title><style type="text/css">h1{color:white;font-size: 24pt;text-align: center;font-family: Arial,sans-serif;}.menu{color: white  ;font-size: 12pt;text-align: center;font-family: Arial,sans-serif;font-weight: bold;}td{background: black;}p{color: black;font-size: 12pt;text-align: justify;font-family: Arial,sans-serif;font-weight: bold;}a:link,a:visited,a:active{color: white;}</style>
</head>
<body>
<!-- page header -->
<table width="100%" cellpadding="12" cellspacing="0" border="0"><tr bgcolor="black"><td align="left"><img src="logo.gif" alt="TLA logo" height=70 width=70></td><td><h1>TLA Consulting</h1></td><td align="right"><img src="logo.gif" alt="TLA logo" height=70 width=70></td></tr>
</table><!-- menu -->
<table width="100%" bgcolor="white" cellpadding="4" cellspacing="4"><tr ><td width="25%"><img src="s-logo.gif" alt="" height=20 width=20> <span class="menu">Home</span></td><td width="25%"><img src="s-logo.gif" alt="" height=20 width=20> <span class="menu">Contact</span></td><td width="25%"><img src="s-logo.gif" alt="" height=20 width=20> <span class="menu">Services</span></td><td width="25%"><img src="s-logo.gif" alt="" height=20 width=20> <span class="menu">Site Map</span></td></tr>
</table>

我们注意到,这里相当于从home.html切了一部分

3.footer.inc包含页面底部脚注处显示的表格

footer.inc —— 所有TLA网站的页面可重复使用的脚注

<!-- page footer -->
<table width="100%" bgcolor="black" cellpadding="12" border="0"><tr><td><p class="foot">&copy; TLA Consulting Pty Ltd.</p><p class="foot">Please see our <a href="legal.php">legal information page</a></p></td></tr>
</table>
</body>
</html>

4.运行home.php文件,效果与home.html一样

运行结果:

使用上述方法很容易就使网站拥有统一的风格。

(注意:书上的文件后缀前后不同,引用的文件要与目录中存在的文件一致)

最重要的是,用这种方法,我们也很容易修改脚注和页眉(只需要进行一次修改)。

******************** 5.2.3 使用auto_prepend_file 和 auto_append_file

在配置文件php.ini中有两个选项:auto_prepend_file , auto_append_file,通过这两个选项来设置页眉和脚注,可以

保证它们在每个页面的前后被载入,如果载入的文件不存在,则产生警告。

如果使用了这些指令,就不需要输入include()语句,但页眉和脚注不再是页面的可选内容。

(一般不使用这种方法)

转载于:https://www.cnblogs.com/yojiaku/p/5728741.html

第五章 代码重用与函数编写(1)相关推荐

  1. PHP代码重用与函数编写

    代码重用与函数编写 1.使用require()和include()函数 这两个函数的作用是将一个文件爱你载入到PHP脚本中,这样就可以直接调用这个文件中的方法. require()和include() ...

  2. 第五章 代码的可复用性——复用性的结构

    1.行为子类型与LSP(Liskov Substitution Principle) 行为子类型: 子类型多态:客户端可用统一的方式处理不同类型的对象. 栗子! 在java中编译器关于这部分有以下规则 ...

  3. 《无人驾驶车辆模型预测控制第一版》第五章代码(下)

    本代码基于无人驾驶车辆模型预测控制第一版本第五章中的代码予以纠正,下面代码已经过测试. 通过函数来生成参考轨迹(双移线),然后用模型预测控制器去跟踪. 需要会员课程的朋友可以点击文末小卡片 加入 HU ...

  4. Day4_代码重用与函数

    知识点速记: 重用代码的方法:脚本包含require().include(); 全局配置文件php.ini(auto_prepend_file/auto_append_file); 目录配置文件.ht ...

  5. Python-第五周-代码复用与函数递归-02

    1.代码复用: 简而言之,就是重复使用该代码,例如 多次调用一个函数. . . 2.模块化设计: 3.函数递归: 基例:就是求 n 阶层时 n=1 的那部分. 链条:就是求 n 阶层时 n不为1 的那 ...

  6. 第十九章 代码重用 5包含对系统的消耗

    /* //5 包含对系统的消耗 //由于book类的数据成员中包含量了3个String类的对像,因此在创建一个book类对像时也就不可避免的创建3个String类的对像,本节我们通过在String类和 ...

  7. 莫烦Pytorch神经网络第五章代码修改

    5.1动态Dynamic import torch from torch import nn import numpy as np import matplotlib.pyplot as plt# t ...

  8. Tomcat:第五章:Tomcat 部署脚本编写

    我们平时启动 Tomcat 过程是怎么样的? 复制WAR包至Tomcat webapp 目录. 执行starut.bat 脚本启动. 启动过程中war 包会被自动解压装载. 但是我们在 Eclipse ...

  9. 第五章 函数和代码复用

    第五章 函数和代码复用 5.1 函数的基本使用 5.1.1 函数的定义 定义:函数是一段具有特定功能的.可重用的语句组,用函数名来表示并通过函数名进行功能调用. 使用函数的目的:降低编程难度和代码重用 ...

  10. 20131005第四章,第五章内容整理与归纳。

    第四章是讲循环结构的,而其应用最多的是while语句.其一般形式为:while(表达式) 循环体语句: for语句和while语句都能实现循环.一般情况下,如果题目中指定了循环次数,使用for语句更清 ...

最新文章

  1. (转) Twisted :第二十一部分 惰性不是迟缓: Twisted和Haskell
  2. 电脑CPU依然得看英特尔酷睿:新一代性能提升20%,AI能力翻5倍;网友:感谢AMD...
  3. C/C++中各种类型int、long、double、char表示范围(最大最小值)
  4. Vue_(组件通讯)动态组件结合keep-alive
  5. 编码不一致问题-Illegal mix of collations
  6. InnoDB: Error: log file .\ib_logfile0 is of different size 0 10485760 bytes
  7. 【dmp文件还原到oralce数据库】
  8. 由event target引发的关于事件流的一连串思考(二)
  9. Java和ABAP中的几种引用类型的分析和比较
  10. 通讯故障_PLC与变频器通讯故障处理实例
  11. k8s selector_Kubernetes 服务选择(selector)
  12. vector 实现二维数组
  13. r语言代码html,R语言的R Markdown创建html
  14. [3]⾃自定义视图、视图控制器
  15. 魔方矩阵c语言,C语言检验并打印魔方矩阵,检验并打印魔方矩阵,用C语言,求大神尽快解决...
  16. [转]富人的28个理财习惯
  17. 基于穿戴式智能化步态分析仪的步态分析
  18. flutter 漂亮聊天UI界面实现 flutter-chatUI-again (11)
  19. 基于Master-DistributedMaster-Slave架构的replication
  20. Cadence IC617工艺库安装步骤

热门文章

  1. IE7 绝对定位z-index问题
  2. SQL Server内部的内存管理
  3. HTML5给Web带来9大改变
  4. TensorFlow 学习------第二天
  5. ACLEMNLP'21 | 基于神经转移模型的论辩挖掘任务
  6. Linux基础—3.Linux基础命令总结【有图有真相】
  7. 1024程序员:算法仓鼠创业
  8. 今年最值得期待的JavaScript传奇绿皮书登场!
  9. 经典视觉SLAM框架
  10. 工业标准的品质也已成为开源世界中的范例之一